10

我正在尝试创建一个调试器可视化器,它将显示任何Control. 已完成,但我收到异常"Type is notmarked as serializable"

我该如何克服呢?Control 是一种 .NET Windows Forms 框架类型,我不能将其标记为可序列化。

4

1 回答 1

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")]
于 2010-06-02T17:56:06.553 回答