我有一个IClaimsPrincipal变量,我想看看其中有多少个声明。浏览监视窗口中的属性很复杂,因此我想自定义此对象的显示方式。
我知道[DebuggerTypeProxy] attribute,它最初看起来可以做我想要的事情。不幸的是,它需要附加到类中,而我并不“拥有”这个类。在本例中,它是一个Microsoft.IdentityModel.Claims.ClaimsPrincipal。
我想要显示IClaimsPrincipal.Identities[0].Claims.Count的值。
有没有办法,使用[DebuggerTypeProxy]或类似的方法,自定义我不拥有的类型的值在监视窗口中的显示方式?
发布于 2018-04-05 22:20:04
应用于KeyValuePair的仅显示值成员的DebuggerTypeProxyAttribute示例:
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上测试
发布于 2012-12-18 17:44:21
到目前为止,我想出的最好的方法是调用一个方法:
public static class DebuggerDisplays
{
    public static int ClaimsPrincipal(IClaimsPrincipal claimsPrincipal)
    {
        return claimsPrincipal.Identities[0].Claims.Count;
    }
}...from监视窗口:
DebuggerDisplays.ClaimsPrincipal(_thePrincipal),ac = 10该",ac" suppresses表示“此表达式会产生副作用,不会进行评估”。
但是,请注意,当这超出范围时,Visual Studio将简单地灰显监视窗口条目,即使带有",ac“。为了避免这种情况,您需要确保所有内容都是完全限定的,这意味着您将在监视窗口中看到非常长的表达式。
发布于 2012-12-22 16:37:52
我自己也有很多次同样的需求,所以我在我与人合著的商业工具"BugAid“中创建了一个名为Custom Expressions的功能。使用它,右键单击值,选择“添加自定义表达式”,然后键入它:

注意,您甚至可以为表达式输入一个友好的名称,即"Amount of Claims":
输入自定义表达式后,无论何时将鼠标悬停在变量上,都可以看到其结果:

https://stackoverflow.com/questions/13929956
复制相似问题