我正在尝试创建一个调试器可视化器,显示任何控件的控制层次结构.已经完成,但是我收到了异常“Type not not as serializable”.
解决方法
您还需要实现一个
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")]