调试器Visualizer - 放大镜没有显示

时间:2013-03-27 17:40:39

标签: c# visual-studio-2010 visual-studio

我已经开始编写调试器可视化工具库,但它没有按预期工作。

当我将鼠标悬停在我为其创建可视化器的变量上时,放大镜应该在那里,但事实并非如此。显然,visual studio并没有告诉我出了什么问题。

以下代码驻留在可视化工具库(面向4.0的类库项目)中。

Dialog Debugger:

[assembly: DebuggerVisualizer(
typeof(ColorRGBA8Visualizer),
typeof(Serializer<ColorRGBA8>),
Target = typeof(ColorRGBA8),
Description = "ColorRGBA8 Visualizer")]

public class ColorRGBA8Visualizer
    : DialogDebuggerVisualizer
{
    public static void TestShowVisualizer(object objectToVisualize)
    {
        var visualizerHost = new VisualizerDevelopmentHost(objectToVisualize,
            typeof(ColorRGBA8Visualizer),
            typeof(Serializer<ColorRGBA8>));
        visualizerHost.ShowVisualizer();
    }

    protected override void Show(IDialogVisualizerService windowService, IVisualizerObjectProvider objectProvider)
    {
        var color = Serializer<ColorRGBA8>.GetValue(objectProvider);
        using (var form = new Form())
        {
            form.Text = "Color Visualizer";
            form.BackColor = Color.FromArgb(255, color.R, color.G, color.B);
            form.Size = new Size(100, 10);
            windowService.ShowDialog(form);
        }
    }
}

自定义序列化程序因为我需要它来处理更复杂的对象:

public class Serializer<T>
    : VisualizerObjectSource
{
    public static T GetValue(IVisualizerObjectProvider provider)
    {
        var serializer = new DataContractSerializer(typeof (T));
        var transport = (MemoryStream) provider.GetObject();
        var value = serializer.ReadObject(transport);

        return (T) value;
    }

    public override void GetData(object target, Stream outgoingData)
    {
        var value = (T) target;
        var transport = new MemoryStream();
        var serializer = new DataContractSerializer(typeof(T));
        serializer.WriteObject(transport, value);
        transport.Position = 0;

        base.GetData(transport, outgoingData);
    }
}

我的测试控制台应用程序:

internal class Program
{
    #region Static Methods

    private static void Main(string[] args)
    {
        var value = new ColorRGBA8{R=255};
        ColorRGBA8Visualizer.TestShowVisualizer(value);
    }

    #endregion
}

对话框显示正常,但整个该死的重点是在此时将鼠标悬停在变量“value”上时显示对话框。

是的,我的可视化工具库输出'到'C:\ Program Files(x86)\ Microsoft Visual Studio 10.0 \ Common7 \ Packages \ Debugger \ Visualizers“通过构建库:所有必需的引用也被复制。

Here's what it looks like in my debugger

That's what I expected it to look like

有人能告诉我我错过了什么吗?

修改

这是JaredPar建议的解决方案。由于某些未知原因,visual studio拒绝使用通用序列化程序,即使它们被指定为封闭类型,例如Serializer。

public class ColorRGBA8Serializer
    : VisualizerObjectSource
{
    public static ColorRGBA8 GetValue(IVisualizerObjectProvider provider)
    {
        var serializer = new DataContractSerializer(typeof(ColorRGBA8));
        var transport = (MemoryStream)provider.GetObject();
        var value = serializer.ReadObject(transport);

        return (ColorRGBA8)value;
    }

    public override void GetData(object target, Stream outgoingData)
    {
        var value = (ColorRGBA8)target;
        var transport = new MemoryStream();
        var serializer = new DataContractSerializer(typeof(ColorRGBA8));
        serializer.WriteObject(transport, value);
        transport.Position = 0;

        base.GetData(transport, outgoingData);
    }
}

1 个答案:

答案 0 :(得分:3)

问题似乎是您对DebuggerVisualizerAttribute的第二个参数使用泛型类型。可视化系统在泛型方面受到限制,并且由于某种原因,它似乎在这个位置拒绝通用。我能够使用您的代码显示表单,只将可视化器源参数更改为非泛型

注意:我没有很好的解释为什么行为是这样的。它在逻辑上没有意义,但实验上它没有结果