Get all properties which marked certain attribute

后端 未结 1 1327
死守一世寂寞
死守一世寂寞 2020-11-29 21:39

I have class and properties in there. Some properties can be marked attribute (it\'s my LocalizedDisplayName inherits from DisplayNameAttribute). T

相关标签:
1条回答
  • 2020-11-29 22:04

    It's probably easiest to use IsDefined:

    var properties = type.GetProperties()
        .Where(prop => prop.IsDefined(typeof(LocalizedDisplayNameAttribute), false));
    

    To get the values themselves, you'd use:

    var attributes = (LocalizedDisplayNameAttribute[]) 
          prop.GetCustomAttributes(typeof(LocalizedDisplayNameAttribute), false);
    
    0 讨论(0)
提交回复
热议问题