How to access the Description attribute on either a property or a const in C#?

前端 未结 4 1413
死守一世寂寞
死守一世寂寞 2020-12-15 05:33

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

public static class Group
{

    [Description( \"Specified parent-child rel         


        
相关标签:
4条回答
  • 2020-12-15 06:18

    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);
    
    0 讨论(0)
  • 2020-12-15 06:28

    Try the following

    var property = typeof(Group).GetProperty("UserExistsInGroup");
    var attribute = property.GetCustomAttributes(typeof(DescriptionAttribute), true)[0];
    var description = (DescriptionAttribute)attribute;
    var text = description.Description;
    
    0 讨论(0)
  • 2020-12-15 06:31

    Okay, I've seen your edit, I'm not sure you can do it with extension methods, as they would be anaware of the type of the containing class.

    This is going to sound a little wacky, but how about creating a new class a "DescribedInt", which would have an implicit cast operator to let you use it as an int automatically? You'll be able to use pretty much how you describe. You'll still have a description, but when you need to use it like an Int, you wont' need to get the .Data property...

    eg:

    private void ExampleUse()
    {
        int myvalue = Group.A; //see, no need to cast or say ".Data" - implicit cast
        string text = Group.A.Description;
    

    //do stuff with values... }

    public static class Group
    {
        public static DescribedInt A = new DescribedInt(12, "some description");
        public static DescribedInt B = new DescribedInt(88, "another description");
    }
    
    public class DescribedInt
    {
        public readonly int data;
        public readonly string Description;
    
        public DescribedInt(int data, string description)
        {
            this.data = data;
            this.Description = description;
        }
    
        //automatic cast to int
        public static implicit operator int(DescribedInt orig)
        {
            return orig.data;
        }
    
        //public DescribedInt(string description)
        //{
        //    this.description = description;
        //}
    
        //if you ever need to go the "other way"
        //public static implicit operator DescribedInt(int orig)
        //{
        //    return new DescribedInt(orig, "");
        //}
    }
    
    0 讨论(0)
  • 2020-12-15 06:39

    Here is a helper class I am using for processing custom attributes in .NET

    public class AttributeList : List<Attribute>
    {
        /// <summary>
        /// Gets a list of custom attributes
        /// </summary>
        /// <param name="propertyInfo"></param>
        /// <returns></returns>
        public static AttributeList GetCustomAttributeList(ICustomAttributeProvider propertyInfo)
        {
            var result = new AttributeList();
            result.AddRange(propertyInfo.GetCustomAttributes(false).Cast<Attribute>());
            return result;
        }
    
        /// <summary>
        /// Finds attribute in collection by its type
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public T FindAttribute<T>() where T : Attribute
        {
            return (T)Find(x => typeof(T).IsAssignableFrom(x.GetType()));
        }
    
        public bool IsAttributeSet<T>() where T : Attribute
        {
            return FindAttribute<T>() != null;
        }
    }
    

    Also unit tests for MsTest showing how to use this class

    [TestClass]
    public class AttributeListTest
    {
        private class TestAttrAttribute : Attribute
        {
        }
    
        [TestAttr]
        private class TestClass
        {
        }
    
        [TestMethod]
        public void Test()
        {
            var attributeList = AttributeList.GetCustomAttributeList(typeof (TestClass));
            Assert.IsTrue(attributeList.IsAttributeSet<TestAttrAttribute>());
            Assert.IsFalse(attributeList.IsAttributeSet<TestClassAttribute>());
            Assert.IsInstanceOfType(attributeList.FindAttribute<TestAttrAttribute>(), typeof(TestAttrAttribute));
        }
    }
    

    http://www.kozlenko.info/2010/02/02/getting-a-list-of-custom-attributes-in-net/

    0 讨论(0)
提交回复
热议问题