gpt4 book ai didi

c# - 调试器可视化工具和 "Type is not marked as serializable"

转载 作者:可可西里 更新时间:2023-11-01 08:59:53 25 4
gpt4 key购买 nike

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

我该如何克服呢?控件是 .NET Windows 窗体框架类型,我无法将其标记为可序列化。

最佳答案

您还需要实现 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")]

关于c# - 调试器可视化工具和 "Type is not marked as serializable",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2959048/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com