创建一个对所有对象进行操作的 C# VS2010 Visualizer

发布于 2024-12-09 00:35:07 字数 1280 浏览 0 评论 0原文

我正在尝试创建一个可以对所有对象执行可视化的 C# 调试可视化工具。我似乎无法获得程序集属性(在命名空间之上)来将此可视化工具绑定到 System.Object,就像我能够与系统中的其他对象一样。我进行了详细搜索,但没有找到任何有关为所有对象创建可视化工具的示例/讨论。这是我试图工作的代码,当绑定到 String 或 Int32 时它工作得很好,但绑定到 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());
        }
    }
}
}

作为一名使用 IntelliJ 的前 Java 程序员,我习惯于能够在调试模式下查看特定引用所指向的堆地址。这使您可以一眼看出两个对象是否引用相等。此外,还有一些其他事情值得了解,但解释起来可能有点冗长。如果我能让它工作,我会发布最终的代码。

那么有人知道如何让可视化工具对所有对象都处于活动状态吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

山川志 2024-12-16 00:35:07

我不知道你的代码有什么问题。
但是@Bismark,目标不必能够序列化,因为您可以使用自己的 VisualizerObjectSource 对其进行序列化,

我建议您将 .GetType().AsseblyQualifierName 序列化它,这将允许您知道流包含什么类型的对象,因此在反序列化时您知道您的对象实际上是类 x 的实例,我使用了这种技术我自己的可视化工具,因为有时您可能会序列化类的子类型,而在反序列化时您不知道自己在什么时间工作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文