首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我可以在我不拥有的类型上使用像DebuggerTypeProxyAttribute这样的东西吗?

我可以在我不拥有的类型上使用像DebuggerTypeProxyAttribute这样的东西吗?
EN

Stack Overflow用户
提问于 2012-12-18 17:33:53
回答 3查看 433关注 0票数 4

我有一个IClaimsPrincipal变量,我想看看其中有多少个声明。浏览监视窗口中的属性很复杂,因此我想自定义此对象的显示方式。

我知道[DebuggerTypeProxy] attribute,它最初看起来可以做我想要的事情。不幸的是,它需要附加到类中,而我并不“拥有”这个类。在本例中,它是一个Microsoft.IdentityModel.Claims.ClaimsPrincipal

我想要显示IClaimsPrincipal.Identities[0].Claims.Count的值。

有没有办法,使用[DebuggerTypeProxy]或类似的方法,自定义我不拥有的类型的值在监视窗口中的显示方式?

EN

回答 3

Stack Overflow用户

发布于 2018-04-05 22:20:04

应用于KeyValuePair的仅显示值成员的DebuggerTypeProxyAttribute示例:

代码语言:javascript
运行
复制
using System.Collections.Generic;
using System.Diagnostics;

[assembly: DebuggerTypeProxy(typeof(ConsoleApp2.KeyValuePairDebuggerTypeProxy<,>), Target = typeof(KeyValuePair<,>))]
// alternative format [assembly: DebuggerTypeProxy(typeof(ConsoleApp2.KeyValuePairDebuggerTypeProxy<,>), TargetTypeName = "System.Collections.Generic.KeyValuePair`2")]

namespace ConsoleApp2
{
    class KeyValuePairDebuggerTypeProxy<TKey, TValue>
    {
        private KeyValuePair<TKey, TValue> _keyValuePair; // beeing non-public this member is hidden
        //public TKey Key => _keyValuePair.Key;
        public TValue Value => _keyValuePair.Value;

        public KeyValuePairDebuggerTypeProxy(KeyValuePair<TKey, TValue> keyValuePair)
        {
            _keyValuePair = keyValuePair;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var dictionary = new Dictionary<int, string>() { [1] = "one", [2] = "two" };
            Debugger.Break();
        }
    }
}

在Visual Studio 2017上测试

票数 5
EN

Stack Overflow用户

发布于 2012-12-18 17:44:21

到目前为止,我想出的最好的方法是调用一个方法:

代码语言:javascript
运行
复制
public static class DebuggerDisplays
{
    public static int ClaimsPrincipal(IClaimsPrincipal claimsPrincipal)
    {
        return claimsPrincipal.Identities[0].Claims.Count;
    }
}

...from监视窗口:

代码语言:javascript
运行
复制
DebuggerDisplays.ClaimsPrincipal(_thePrincipal),ac = 10

",ac" suppresses表示“此表达式会产生副作用,不会进行评估”。

但是,请注意,当这超出范围时,Visual Studio将简单地灰显监视窗口条目,即使带有",ac“。为了避免这种情况,您需要确保所有内容都是完全限定的,这意味着您将在监视窗口中看到非常长的表达式。

票数 1
EN

Stack Overflow用户

发布于 2012-12-22 16:37:52

我自己也有很多次同样的需求,所以我在我与人合著的商业工具"BugAid“中创建了一个名为Custom Expressions的功能。使用它,右键单击值,选择“添加自定义表达式”,然后键入它:

注意,您甚至可以为表达式输入一个友好的名称,即"Amount of Claims":

输入自定义表达式后,无论何时将鼠标悬停在变量上,都可以看到其结果:

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13929956

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档