调试器可视和"类型未标记为可序列" [英] Debugger Visualizer and "Type is not marked as serializable"

查看:121
本文介绍了调试器可视和"类型未标记为可序列"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个可视化调试器将显示任何控制控制层次。它这样做,但我得到异常的类型未标记为可序列化

I am trying to create a debugger visualizer that would show control hierarchy for any Control. It's done but I'm getting the exception "Type is not marked as serializable".

我如何克服?控制是一个.NET Windows窗体框架的类型,我不能将其标记为可序列化。

How do I overcome that? Control is a .NET Windows Forms framework type, I can't mark it as serializable.

推荐答案

您需要同时实施的 VisualizerObjectSource 执行自定义序列化

You'll need to also implement a VisualizerObjectSource to perform custom serialization.

例如:

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 ,这对于这个例子可能是这样的:

You'll also need to register the visualizer with the appropriated VisualizarObjectSource, which for this example could be something like this:

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

这篇关于调试器可视和"类型未标记为可序列"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆