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

寵の児 提交于 2019-11-28 08:46:45

问题


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?


回答1:


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.




回答2:


List<T> uses the DebuggerTypeProxyAttribute to define a proxy for the debugger. You can find all using the following expression:

var types =
    from assembly in AppDomain.CurrentDomain.GetAssemblies()
    from type in assembly.GetTypes()
    from attribute in type.GetCustomAttributes(typeof(DebuggerTypeProxyAttribute), true)
    select ((DebuggerTypeProxyAttribute)attribute).ProxyTypeName;

With the above expression, you can also test for DebuggerVisualizerAttribute, but this gave zero results (probably because the assemblies in the specific folder are not referenced). If you reference the assemblies in the folder, you can use the above expression to find the implementations there.




回答3:


There is a nice article about how to make your own List debugger:

http://www.codeproject.com/KB/debug/DebugIList.aspx




回答4:


You can add an attribute to you class System.Diagnostics.DebuggerVisualizerAttribute with the visualizer type in argument



来源:https://stackoverflow.com/questions/4886836/what-debuggervisualizers-are-already-existing-within-visualstudio-or-net-framew

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!