Get the description attributes At class level

前端 未结 3 914
梦谈多话
梦谈多话 2021-02-05 07:08

I have such a class

[Description(\"This is a wahala class\")]
public class Wahala
{

}

Is there anyway to get the content of the Descript

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-05 07:59

    You can use reflection to read attribute data:

    System.Reflection.MemberInfo inf = typeof(Wahala);
    object[] attributes;
    attributes = 
       inf.GetCustomAttributes(
            typeof(DescriptionAttribute), false);
    
    foreach(Object attribute in attributes)
    {
        DescriptionAttribute da = (DescriptionAttribute)attribute;
        Console.WriteLine("Description: {0}", da.Description);
    }
    

    Adapted from here.

提交回复
热议问题