<i id='r3UDs'><tr id='r3UDs'><dt id='r3UDs'><q id='r3UDs'><span id='r3UDs'><b id='r3UDs'><form id='r3UDs'><ins id='r3UDs'></ins><ul id='r3UDs'></ul><sub id='r3UDs'></sub></form><legend id='r3UDs'></legend><bdo id='r3UDs'><pre id='r3UDs'><center id='r3UDs'></center></pre></bdo></b><th id='r3UDs'></th></span></q></dt></tr></i><div id='r3UDs'><tfoot id='r3UDs'></tfoot><dl id='r3UDs'><fieldset id='r3UDs'></fieldset></dl></div>

<tfoot id='r3UDs'></tfoot>

  1. <small id='r3UDs'></small><noframes id='r3UDs'>

      <bdo id='r3UDs'></bdo><ul id='r3UDs'></ul>

  2. <legend id='r3UDs'><style id='r3UDs'><dir id='r3UDs'><q id='r3UDs'></q></dir></style></legend>

      如何在 C# 中访问属性或 const 的描述属性?

      时间:2023-07-27

      <i id='U7x4R'><tr id='U7x4R'><dt id='U7x4R'><q id='U7x4R'><span id='U7x4R'><b id='U7x4R'><form id='U7x4R'><ins id='U7x4R'></ins><ul id='U7x4R'></ul><sub id='U7x4R'></sub></form><legend id='U7x4R'></legend><bdo id='U7x4R'><pre id='U7x4R'><center id='U7x4R'></center></pre></bdo></b><th id='U7x4R'></th></span></q></dt></tr></i><div id='U7x4R'><tfoot id='U7x4R'></tfoot><dl id='U7x4R'><fieldset id='U7x4R'></fieldset></dl></div>
    1. <small id='U7x4R'></small><noframes id='U7x4R'>

        <bdo id='U7x4R'></bdo><ul id='U7x4R'></ul>

      • <legend id='U7x4R'><style id='U7x4R'><dir id='U7x4R'><q id='U7x4R'></q></dir></style></legend>

            <tbody id='U7x4R'></tbody>
          <tfoot id='U7x4R'></tfoot>

                本文介绍了如何在 C# 中访问属性或 const 的描述属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                如何访问 const 或属性上的 Description 属性,即,

                How do you access the Description property on either a const or a property, i.e.,

                public static class Group
                {
                
                    [Description( "Specified parent-child relationship already exists." )]
                    public const int ParentChildRelationshipExists = 1;
                
                    [Description( "User is already a member of the group." )]
                    public const int UserExistsInGroup = 2;
                
                }
                

                public static class Group
                {
                
                    [Description( "Specified parent-child relationship already exists." )]
                    public static int ParentChildRelationshipExists { 
                      get { return 1; } 
                    }
                
                    [Description( "User is already a member of the group." )]
                    public static int UserExistsInGroup { 
                      get { return 2; } 
                    }
                
                }
                

                在调用类中,我想访问 Description 属性,即,

                In the calling class I'd like to access the Description property, i.e.,

                int x = Group.UserExistsInGroup;
                string description = Group.UserExistsInGroup.GetDescription(); // or similar
                

                我也对其他方法的想法持开放态度.

                I'm open to ideas to other methodologies as well.

                我应该提到我已经看到这里提供的一个例子:自动实现的属性是否支持属性?

                I should have mentioned that I've seen an example provided here: Do auto-implemented properties support attributes?

                但是,我正在寻找一种无需在属性类型中输入字符串文字即可访问描述属性的方法,即,我宁愿不这样做:

                However, I'm looking for a method to access the description attribute without having to enter a string literal into the property type, i.e., I'd rather not do this:

                typeof(Group).GetProperty("UserExistsInGroup");
                

                类似于扩展方法的东西;类似于以下方法,它将通过扩展方法返回 Enum 上的 Description 属性:

                Something along the lines of an Extension Method; similar to the following method that will return the Description attribute on an Enum via an Extension Method:

                public static String GetEnumDescription( this Enum obj )
                {
                    try
                    {
                        System.Reflection.FieldInfo fieldInfo = 
                            obj.GetType().GetField( obj.ToString() );
                
                        object[] attribArray = fieldInfo.GetCustomAttributes( false );
                
                        if (attribArray.Length > 0)
                        {
                            var attrib = attribArray[0] as DescriptionAttribute;
                
                            if( attrib != null  )
                                return attrib.Description;
                        }
                        return obj.ToString();
                    }
                    catch( NullReferenceException ex )
                    {
                        return "Unknown";
                    }
                }
                

                推荐答案

                您可以调用MemberInfo.GetCustomAttributes() 以获取在 Type 的成员上定义的任何自定义属性.您可以通过执行以下操作获取属性的 MemberInfo:

                You can call MemberInfo.GetCustomAttributes() to get any custom attributes defined on a member of a Type. You can get the MemberInfo for the property by doing something like this:

                PropertyInfo prop = typeof(Group).GetProperty("UserExistsInGroup",
                    BindingFlags.Public | BindingFlags.Static);
                

                这篇关于如何在 C# 中访问属性或 const 的描述属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:使用泛型的 xUnit 理论测试 下一篇:获取描述属性在类级别

                相关文章

              • <small id='ao0DX'></small><noframes id='ao0DX'>

                1. <i id='ao0DX'><tr id='ao0DX'><dt id='ao0DX'><q id='ao0DX'><span id='ao0DX'><b id='ao0DX'><form id='ao0DX'><ins id='ao0DX'></ins><ul id='ao0DX'></ul><sub id='ao0DX'></sub></form><legend id='ao0DX'></legend><bdo id='ao0DX'><pre id='ao0DX'><center id='ao0DX'></center></pre></bdo></b><th id='ao0DX'></th></span></q></dt></tr></i><div id='ao0DX'><tfoot id='ao0DX'></tfoot><dl id='ao0DX'><fieldset id='ao0DX'></fieldset></dl></div>

                  <legend id='ao0DX'><style id='ao0DX'><dir id='ao0DX'><q id='ao0DX'></q></dir></style></legend>
                2. <tfoot id='ao0DX'></tfoot>

                    <bdo id='ao0DX'></bdo><ul id='ao0DX'></ul>