Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Attribute.isDefined() example?

Tags:

c#

metadata

Can someone please give me an example of using Attribute.isDefined() to check if a particular custom attribute has been applied to a given class?

I've checked msdn, but only see possiblities for attributes applied to assemblies, members etc. I'm also open to alternative methods for achieving the same thing!

like image 505
UpTheCreek Avatar asked Jun 11 '10 19:06

UpTheCreek


1 Answers

There doesn't seem to be an overload of Attribute.IsDefined that takes a Type.

Instead, You can call Type.GetCustomAttributes:

if (typeof(SomeClass).GetCustomAttributes(typeof(SomeAttribute), false).Length > 0)
like image 117
SLaks Avatar answered Sep 23 '22 09:09

SLaks