DebuggerDisplay 属性可以应用于不拥有的类型吗? [英] Can the DebuggerDisplay attribute be applied to types one doesn't own?

查看:11
本文介绍了DebuggerDisplay 属性可以应用于不拥有的类型吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢 DebuggerDisplay 属性.我非常喜欢它,所以我想在我没有源代码的类型上使用它.

I like the DebuggerDisplay attribute. I like it so much, that I want to use it on types that I don't have the source code for.

这可能吗?

推荐答案

Example of setting DebuggerDisplay for a external type (System.Collections.Generic.KeyValuePair) 将以下内容添加到 AssemblyInfo.cs:

Example of setting DebuggerDisplay for a foreign type (System.Collections.Generic.KeyValuePair<TKey,TValue>) add the following to AssemblyInfo.cs:

using System.Collections.Generic;
using System.Diagnostics;

[assembly: DebuggerDisplay("[Key={Key}, Value={Value}]", Target = typeof(KeyValuePair<,>))]

(在 VS2015 中测试)

(Tested in VS2015)

编辑 2020:

无法为 KeyValuePair<、> 重现上述内容在 VS2019 中,但它似乎与 KeyValuePair<,> 相关.

Was not able to reproduce the above for KeyValuePair<,> in VS2019 but it seems to be related to KeyValuePair<,>.

对于非拥有类型的私有成员尝试这样的事情

For private member of non owned types try something like this

ClassLibrary1:

ClassLibrary1:

//using System.Diagnostics;

namespace ClassLibrary1
{
    //[DebuggerDisplay("Foo.Bar={Bar}")] // works too for types you own
    public class Foo
    {
        private int Bar = 42;
    }
}

控制台应用程序1:

using System.Diagnostics;
using System.Reflection;
using ClassLibrary1;

[assembly: DebuggerDisplay("Foo.Bar={FooDebuggerDisplay.Bar(this)}", Target=typeof(Foo))]

class FooDebuggerDisplay
{
    public static int Bar(Foo foo) => (int)foo.GetType().GetField("Bar",BindingFlags.Instance|BindingFlags.NonPublic).GetValue(foo);
}

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var foo = new Foo();
            Debugger.Break();
        }
    }
}

(在 VS2019 中测试)

(Tested in VS2019)

这篇关于DebuggerDisplay 属性可以应用于不拥有的类型吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆