调试器可视和"类型未标记为可序列" [英] Debugger Visualizer and "Type is not marked as serializable"
问题描述
我想创建一个可视化调试器将显示任何控制
控制层次。它这样做,但我得到异常的类型未标记为可序列化的
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屋!