views:

33

answers:

3

I'm trying to determine if a interface is decorated with a specific attribute. For example, I have the following interface:

<MyCustomAttribute()> _
Public Interface IMyInterface
    Function Function1
    Sub DeleteWorkflowInstanceMap(ByVal instanceId As Guid)
    Sub InsertWorkflowInstanceMap(ByVal instanceId As Guid, ByVal aliasName As String)
End Interface

How do I determine if IMyInterface is decorated with the MyCustomAttribute attribute?

+2  A: 
GetType(IMyInterface).GetCustomAttributes(GetType(MyCustomAttribute), false).Length > 0

(I hope I have the VB syntax right.) Basically get a Type representing IMyInterface, then call GetCustomAttributes on it passing the type of attribute you're interested in. If that returns a non-empty array, the attribute is present.

itowlson
Perfect... Thank you!
Matt Ruwe
+2  A: 

Even better than GetCustomAttributes is the Shared method IsDefined:

Attribute.IsDefined(GetType(IMyInterface), GetType(MyCustomAttribute))
Marcel Gosselin