简体   繁体   中英

Creating a C# VS2010 Visualizer that operates on all Object

I'm attempting to create a C# debugging visualizer that can perform a visualization on all objects. I can't seem to get the assembly attribute (above the namespace) to bind this visualizer to System.Object like I've 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 I'm 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 I'm 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 I'll 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM