我可以在我不拥有的类型上使用 DebuggerTypeProxyAttribute 之类的东西吗? [英] Can I use something like DebuggerTypeProxyAttribute on a type that I don't own?

查看:21
本文介绍了我可以在我不拥有的类型上使用 DebuggerTypeProxyAttribute 之类的东西吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我知道

在 Visual Studio 2017 上测试

I've got an IClaimsPrincipal variable, and I'd like to see how many claims are in it. Navigating through the properties in the watch window is complicated, so I'd like to customize how this object is displayed.

I'm aware of the [DebuggerTypeProxy] attribute, which initially looked like it might do what I want. Unfortunately, it needs to be attached to the class, and I don't "own" the class. In this case it's a Microsoft.IdentityModel.Claims.ClaimsPrincipal.

I'd like to display the value of IClaimsPrincipal.Identities[0].Claims.Count.

Is there any way, using [DebuggerTypeProxy] or similar, to customize how the value of a type that I don't own is displayed in the watch window?

解决方案

Example of DebuggerTypeProxyAttribute applied to KeyValuePair showing only the Value member:

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();
        }
    }
}

Tested on Visual Studio 2017

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

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