开发者

GetCustomAttributes

开发者 https://www.devze.com 2023-01-15 19:16 出处:网络
<AttributeUsage(AttributeTargets.Property)> _ Private Class CustomAttrib Inherits Attribute Public Property DataTypeLength As Integer
    <AttributeUsage(AttributeTargets.Property)> _
Private Class CustomAttrib
    Inherits Attribute

    Public Property DataTypeLength As Integer

End Class

Private Class TestClass
    <CustomAttrib(DataTypeLength:=75)> _
    Public Property MyProp As String
End Class

Dim properties = GetType(TestClass).GetFields(Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic)

For each Prop As FieldInfo in Properties
开发者_高级运维
    Dim attributes =    DirectCast(prop.GetCustomAttributes(GetType(TestClass), False), CustomAttrib())

  If Attributes.Any Then
    'get value of custom attribute
  End If
Next

Ok no matter what i do attributes is always null/nothing. I have also tried the following also:

Attribute.GetCustomAttributes(prop)

This returns two attributes of type CompilerGeneratedAttribute and DebuggerBrowsableAttribute

What am i doing wrong here?


The problem here is that you've declared an auto-implemented property but are asking for attributes that are defined on fields. The auto-implemented property will have a backing field but the compiler will not copy custom attributes from the property to the backing field. Hence you don't see any.

The way to fix this is to switch the call from GetFields to GetProperties. Additionally as Hans pointed out you need to look for public not private entities and need to look for the custom attribute type CustomAttrib not the class TestClass

Dim properties = GetType(TestClass).GetProperties(Reflection.BindingFlags.Instance Or Reflection.BindingFlags.Public)


Dim properties = GetType(TestClass).GetFields(...)

You're trying to retrieve properties but you used GetFields. Use GetProperties instead.

Next problem is the BindingFlags you pass. You are asking for private properties but the class has only public properties. Also include BindingFlags.Public.

Next problem is the type you pass GetCustomAttributes(), you want to search for CustomAttrib, not the class type.

Fixed:

    Dim properties = GetType(TestClass).GetProperties(Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic Or BindingFlags.Public)
    For Each Prop As PropertyInfo In properties
        Dim attributes = DirectCast(Prop.GetCustomAttributes(GetType(CustomAttrib), False), CustomAttrib())
        If attributes.Length > 0 Then
            ''get value of custom attribute
        End If
    Next


Dim attributes = DirectCast(prop.GetCustomAttributes(GetType(CustomAttrib), False), CustomAttrib())


As JaredPar has quite adequately stated, it's to do with your binding flags. This reveals a general lesson about Reflection - most methods are finicky about what flags you use. Some are not intuitive. If you're having trouble, do play around with the binding flags and try to select all that you think would apply to the type of member you're searching for.

0

精彩评论

暂无评论...
验证码 换一张
取 消