Reflection - get attribute name and value on property

后端 未结 15 2108
谎友^
谎友^ 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:19

    You can use GetCustomAttributesData() and GetCustomAttributes():

    var attributeData = typeof(Book).GetProperty("Name").GetCustomAttributesData();
    var attributes = typeof(Book).GetProperty("Name").GetCustomAttributes(false);
    

提交回复
热议问题