简体   繁体   English

用于 IEnumerable 的 Visual Studio Visualizer<string>

[英]Visual Studio Visualizer for IEnumerable<string>

TL;DR TL; 博士
Does anyone know how to write a Debug Visualizer for Visual Studio 2012, in C#, so I can visualize IEnumerable<string> , string[] or similar objects?有谁知道如何在 C# 中为 Visual Studio 2012 编写调试可视化工具,以便我可以可视化IEnumerable<string>string[]或类似的对象?

More Info更多信息
Visual Studio Debug Visualizer are great, and I use some popular ones ( Mole ) regularly. Visual Studio Debug Visualizer 很棒,我经常使用一些流行的 ( Mole )。 However, now the time has come to roll out some custom visualizers.但是,现在是推出一些自定义可视化工具的时候了。 I started off with a simple visualizer for a string:我从一个简单的字符串可视化工具开始:

[assembly: System.Diagnostics.DebuggerVisualizer(typeof(My.Namespace.DebuggerSide),
                                            typeof(VisualizerObjectSource),
                                            Target = typeof(string),
                                            Description = "Awesome Visualizer")]

the code of DebuggerSide is basically the example from the template: DebuggerSide 的代码基本上是模板中的示例:

public class DebuggerSide : DialogDebuggerVisualizer
{
    protected override void Show(IDialogVisualizerService windowService, IVisualizerObjectProvider objectProvider)
    {
        if (windowService == null)
            throw new ArgumentNullException("windowService");
        if (objectProvider == null)
            throw new ArgumentNullException("objectProvider");


        var data = (string)objectProvider.GetObject();

        using (var displayForm = new VisualizerForm(data))
        {
            windowService.ShowDialog(displayForm);
        }
    }

    /// <summary>
    /// Tests the visualizer by hosting it outside of the debugger.
    /// </summary>
    /// <param name="objectToVisualize">The object to display in the visualizer.</param>
    public static void TestShowVisualizer(object objectToVisualize)
    {
        VisualizerDevelopmentHost visualizerHost = new VisualizerDevelopmentHost(objectToVisualize, typeof(DebuggerSide));
        visualizerHost.ShowVisualizer();
    }
}

VisualizerForm is the custom form with extra controls etc... when I build the project and put the dll in the My Documents/Visual Studio 11/Visualizers folder, and restart visual studio, I can indeed see the debugger appearing under the looking glass icon when a breakpoint is hit for a string object. VisualizerForm是额外的控制自定义表单等等...当我建项目,并将该DLL在My Documents/Visual Studio 11/Visualizers文件夹,重新启动Visual Studio,我确实可以看到镜子图标下的调试出现当为字符串对象命中断点时。 Woohoo!呜呼! So far so good.到现在为止还挺好。

Now I would like to, instead of visualizing string , visualize string[] or IEnumerable<string> or a similar object.现在我想,而不是可视化string ,可视化string[]IEnumerable<string>或类似的对象。 However when I change the assembly attribute to IEnumerable<string> , this is not working, there is not even a looking glass icon displayed on the IEnumerable objects.但是,当我将程序集属性更改为IEnumerable<string> ,这不起作用,IEnumerable 对象上甚至没有显示玻璃图标。

UPDATE更新
I can get it to work by changing the TargetType to List<> and then checking if I can cast to List<string> .我可以通过将 TargetType 更改为List<>然后检查是否可以转换为List<string>来使其工作。 However, this means I have to cast all my objects I want to debug to List and can't use IEnumerable<> or string[]但是,这意味着我必须将要调试的所有对象都转换为List并且不能使用IEnumerable<>或 string[]

Visualizers are documented as可视化器被记录为

Support for generic types is limited.对泛型类型的支持是有限的。 You can write a visualizer for a target that is a generic type only if the generic type is an open type.只有当泛型类型是开放类型时,您才能为泛型类型的目标编写可视化工具。

Which means you cannot write a visualizer that uses a closed constructed type like IEnumerable<string> .这意味着您不能编写使用封闭构造类型(如IEnumerable<string>的可视化工具。 Have you tried setting the target type to IEnumeraable<> then checking to see if the elements are of type string ?您是否尝试将目标类型设置为IEnumeraable<>然后检查元素是否为string类型?

See the 'Objects that can have debugger visualizers' section here: https://rapiddevbookcode.codeplex.com/wikipage?title=EnumerableDebugVisualizer请参阅此处的“可以具有调试器可视化工具的对象”部分: https : //rapiddevbookcode.codeplex.com/wikipage?title=EnumerableDebugVisualizer

My Enumerable Debugger Visualizer will work with an IEnumerable if the underlying concrete type registered with it, you could look at the registration code to get yours working.我的可枚举调试器可视化器将与 IEnumerable 一起使用,如果底层的具体类型注册了它,您可以查看注册代码以使您的工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM