如何访问任意一个属性或在C#中的常量Description属性?属性、常量、Description

2023-09-04 00:11:41 作者:始于心动

如何访问Description属性上无论是常量或属性,即

 公共静态类组
{

    [说明(指定的亲子关系已经存在了。)
    公共const int的ParentChildRelationshipExists = 1;

    [描述(用户已经是该组的成员。)]
    公共const int的UserExistsInGroup = 2;

}
 

 公共静态类组
{

    [说明(指定的亲子关系已经存在了。)
    公共静态INT ParentChildRelationshipExists {
      {返回1; }
    }

    [描述(用户已经是该组的成员。)]
    公共静态INT UserExistsInGroup {
      {返回2; }
    }

}
 

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

  INT X = Group.UserExistsInGroup;
字符串描述= Group.UserExistsInGroup.GetDescription(); //或类似
 
pyqt5 实现右键自定义 每天10分钟,木辛老师带你一步一步学习如何使用PyQt5开发软件...

我打开思路,以其他方法为好。

编辑: 我应该提到,我看到这里提供了一个例子: http://stackoverflow.com/questions/464889/does-auto-implemented-properties-support-attributes

不过,我正在寻找访问描述属性,而无需输入一个字符串到属性类型,即一个方法,我宁愿不这样做:

 的typeof(集团).GetProperty(UserExistsInGroup);
 

沿的扩展方法的东西线;类似于将通过扩展方法返回Description属性的一个枚举下面的方法:

 公共静态字符串GetEnumDescription(此枚举OBJ)
{
    尝试
    {
        System.Reflection.FieldInfo字段信息=
            。obj.GetType()getfield命令(obj.ToString());

        [对象] attribArray = fieldInfo.GetCustomAttributes(假);

        如果(attribArray.Length大于0)
        {
            VAR属性= attribArray [0]作为DescriptionAttribute;

            如果(ATTRIB!= NULL)
                返回attrib.Description;
        }
        返回obj.ToString();
    }
    赶上(的NullReferenceException前)
    {
        返回未知;
    }
}
 

解决方案

您可以调用的 MemberInfo.GetCustomAttributes()工作再上一个键入的成员定义的任何自定义属性。你可以做这样的事情让的MemberInfo 的属性:

 的PropertyInfo道具= typeof运算(集团).GetProperty(UserExistsInGroup
    BindingFlags.Public | BindingFlags.Static);
 

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;

}

or

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

}

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.

EDIT: I should have mentioned that I've seen an example provided here: http://stackoverflow.com/questions/464889/does-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");

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";
    }
}

解决方案

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