Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

visual studio - What DebuggerVisualizers are already existing within VisualStudio or .Net Framework?

A quite dumb question but i simply can't find the answer.

I have a self-written class that implements the IList<T> interface. Now i like to see the containing elements within Debugging like i would with any .Net List<T>.

To get this to work i think i have to provide the correct visualizer within the DebuggerVisualizerAttribute. After a little searching all i could find is the folder for additional Visualizer. But there is just one for the DataSet.

But what are the types of all the Visualizer already available within Visual Studio (e.g. for string, List, etc.), so that i could provide the correct one for my implementation of something already available?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The debugger visualizers used by the .NET framework classes are internal. Which makes them a bit hard to use, you cannot use typeof(). There's a backdoor though, the [DebuggerTypeProxy] attribute also has a constructor that accepts a string. The one you want to use is named Mscorlib_CollectionDebugView, it is capable of visualing any class that implements ICollection<>. Here's an example of usage:

[DebuggerTypeProxy("System.Collections.Generic.Mscorlib_CollectionDebugView`1, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
class MyCollection<T> : IList<T> {
    private List<T> impl = new List<T>();
    public int IndexOf(T item) {  return impl.IndexOf(item); }
    public void Insert(int index, T item) { impl.Insert(index, item); }
    public void RemoveAt(int index) { impl.RemoveAt(index); }
    public T this[int index] {
        get { return impl[index]; }
        set { impl[index] = value; }
    }
    public void Add(T item) { impl.Add(item); }
    public void Clear() { impl.Clear(); }
    public bool Contains(T item) { return impl.Contains(item); }
    public void CopyTo(T[] array, int arrayIndex) { impl.CopyTo(array, arrayIndex); }
    public int Count { get { return impl.Count; }}
    public bool IsReadOnly { get { return ((System.Collections.IList)impl).IsReadOnly; }}
    public bool Remove(T item) { return impl.Remove(item); }
    public IEnumerator<T> GetEnumerator() { return impl.GetEnumerator(); }
    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return GetEnumerator(); }
}

This works for .NET 4.0 as well, even though the version number is wrong. This otherwise has a risk of breaking with the next version of .NET if they decide to rename the internal class.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...