调试器Visualizer和“类型未标记为可序列化”

时间:2010-06-02 15:15:04

标签: c# visual-studio-2008 serialization debuggervisualizer

我正在尝试创建一个调试器可视化工具,它将显示任何Control的控件层次结构。它已经完成,但我得到了异常“类型未标记为可序列化”

我如何克服这个问题? Control是.NET Windows Forms框架类型,我无法将其标记为可序列化。

1 个答案:

答案 0 :(得分:19)

您还需要实施VisualizerObjectSource来执行自定义序列化。

示例:

public class ControlVisualizerObjectSource : VisualizerObjectSource
{
    public override void GetData(object target, Stream outgoingData)
    {
        var writer = new StreamWriter(outgoingData);
        writer.WriteLine(((Control)target).Text);
        writer.Flush();
    }
}
public class ControlVisualizer : DialogDebuggerVisualizer
{
    protected override void Show(
        IDialogVisualizerService windowService,
        IVisualizerObjectProvider objectProvider)
    {
        string text = new StreamReader(objectProvider.GetData()).ReadLine();
    }
    public static void TestShowVisualizer(object objectToVisualize)
    {
        var visualizerHost = new VisualizerDevelopmentHost(
            objectToVisualize,
            typeof(ControlVisualizer),
            typeof(ControlVisualizerObjectSource));
        visualizerHost.ShowVisualizer();
    }
}
class Program
{
    static void Main(string[] args)
    {
        ControlVisualizer.TestShowVisualizer(new Control("Hello World!"));
    }
}

您还需要使用适当的VisualizarObjectSource注册可视化工具,对于此示例,可能是这样的:

[assembly: DebuggerVisualizer(
    typeof(ControlVisualizer),
    typeof(ControlVisualizerObjectSource),
    Target = typeof(System.Windows.Forms.Control),
    Description = "Control Visualizer")]