【问题标题】:How to determine if a class is decorated with a specific attribute如何确定一个类是否用特定属性装饰
【发布时间】:2010-02-11 01:14:27
【问题描述】:

我正在尝试确定界面是否使用特定属性进行装饰。比如我有如下界面:

<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 属性修饰?

【问题讨论】:

    标签: vb.net attributes


    【解决方案1】:

    GetCustomAttributes更好的是Shared方法IsDefined

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

    【讨论】:

      【解决方案2】:
      GetType(IMyInterface).GetCustomAttributes(GetType(MyCustomAttribute), false).Length > 0
      

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

      【讨论】:

        【解决方案3】:
        猜你喜欢
        • 2012-02-06
        • 1970-01-01
        • 2011-03-04
        • 2018-01-12
        • 1970-01-01
        • 2019-08-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多