2010-02-10 55 views
5

我试图确定一个接口是否装饰了特定属性。例如,我有以下接口:如何确定某个类是否装饰了特定属性

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

如何确定是否IMyInterface的与MyCustomAttribute装饰属性?

回答

7

甚至比GetCustomAttributes越好共享方法IsDefined

Attribute.IsDefined(GetType(IMyInterface), GetType(MyCustomAttribute)) 
3
GetType(IMyInterface).GetCustomAttributes(GetType(MyCustomAttribute), false).Length > 0 

(我希望我有VB语法正确。)基本上得到代表IMyInterface的,然后在其上调用GetCustomAttributes传递属性你感兴趣的类型的Type。如果返回一个非空数组,该属性存在。

+0

完美...谢谢! – 2010-02-10 17:13:23

相关问题