当前位置: 动力学知识库 > 问答 > 编程问答 >

visual studio 2010 - Creating a C# VS2010 Visualizer that operates on all Object

问题描述:

Im attempting to create a C# debugging visualizer that can perform a visualization on all objects. I cant seem to get the assembly attribute (above the namespace) to bind this visualizer to System.Object like Ive been able to with other objects in the system. I've searched at length but haven't found any examples/discussion about creating a visualizer for all objects. Here is the code Im trying to get working, it works well enough when bound to String or Int32, but not Object or object.

[assembly: System.Diagnostics.DebuggerVisualizer(

typeof(Visualizers.ObjectVisualizer), typeof(Visualizers.RawObjectScource),

Target = typeof(object), Description = "Object Visualizer")]

namespace Visualizers

{

public class ObjectVisualizer : DialogDebuggerVisualizer

{

override protected void Show(IDialogVisualizerService windowService, IVisualizerObjectProvider objectProvider)

{

Console.Out.WriteLine("InShow");

MessageBox.Show(objectProvider.GetObject().ToString());

}

}

// handle any object, doesn't require that it's Serializable

public class RawObjectScource : VisualizerObjectSource

{

public override void GetData(object target, Stream outgoingData)

{

if (target != null)

{

BinaryFormatter formatter = new BinaryFormatter();

formatter.Serialize(outgoingData, target.ToString());

}

}

}

}

Being a former Java programmer that used IntelliJ Im used to being able to see in debug mode what the heap address is that a specific reference is pointing to. This allows you to see at a glance if two objects are reference equal. Also, there are a few other things that would be valuable to know, but they can be a bit lengthy to explain. If I can get it working Ill post the final code.

So does anyone know how to get a visualizer to be active for all objects?

网友答案:

I dont know what is wrong whit your code. however @Bismark, the target does not have tobe serialize able as you can use your own VisualizerObjectSource to sesialize it

I do suggest you serailise the .GetType().AsseblyQualifierName along whit it, this will allow you to what kind of object the stream contains, so at deserialisation you know that you object is acutaly an instance of class x, I used this technique one of my own visualizers as sometimes you might be serialising an sub-type of of the class whilke at the deserialisation you have no idea what time you are working with.

分享给朋友:
您可能感兴趣的文章:
随机阅读: