Reflection - get attribute name and value on property

后端 未结 15 2062
谎友^
谎友^ 2020-11-22 04:59

I have a class, lets call it Book with a property called Name. With that property, I have an attribute associated with it.

public class Book
{
    [Author(\"         


        
15条回答
  •  醉梦人生
    2020-11-22 05:16

    To get all attributes of a property in a dictionary use this:

    typeof(Book)
      .GetProperty("Name")
      .GetCustomAttributes(false) 
      .ToDictionary(a => a.GetType().Name, a => a);
    

    remember to change from false to true if you want to include inheritted attributes as well.

提交回复
热议问题