-1

I have some attribute that i define. I want to write some generic method that will get where this contain this attribute.

How to implement this ?

Yanshof
  • 9,659
  • 21
  • 95
  • 195
  • This attribute is for classes? You want to seek in your code where this attribute is being used?, give more information plz. – Henrique Apr 12 '16 at 13:55
  • 1
    I dont think this is duplicate. OP wants to know how to check if type contains particular attribute in generic way. – Nitin Apr 12 '16 at 14:01

1 Answers1

1

If with 'attribute' you mean property: That will only work if T is an interface or (base) class / struct. If not, it wouldn't work. You can't check for having a property or field, just if it implements or derives from.

If you don't have a shared interface or base class, your only option is to use dynamic or reflection.

If you mean 'attribute' as in attribute: not possible.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325