jeudi 5 mai 2016

Why do I get "'CustomAttributes' is not a member of 'PropertyInfo'"

I am using reflection to get some property values. The code (appears to) work as intended, but Visual Studio reports error BC30456 'CustomAttributes' is not a member of 'PropertyInfo'. The first time I wrote the code it compiled cleanly; at some later point VS started flagging the error consistently. I have cleaned and rebuilt the solution, closed and re-opened VS several times. When I run the project, it allows me to "run the last successful build" and the page - including any changes elsewhere - executes as expected. The code is part of a WebSite (not web project / web application) under Visual Studio Community 2015, Update 1. The code is in a class within the App_Code folder.

For Each prop As System.Reflection.PropertyInfo In obj.GetType().GetProperties()
    Dim ignore As Boolean = False
    For Each x In prop.CustomAttributes()
        If x.AttributeType.Name = "XmlIgnoreAttribute" Then
            ignore = True
            Exit For
        End If
    Next
 ... other stuff
 Next

error flagged by VS2015

If I use Object Browser, there are no less than 5 instances of System.Reflection.PropertyInfo shown. Each has identical information (MustInherit class PropertyInfo, inherits System.Reflection.MemberInfo, member of System.Reflection) but only 2 include the CustomAttributes property. I'm guessing the compiler is looking at the "wrong" instance of PropertyInfo...?
The code does work so this isn't a show-stopper, but of course I would like my code to compile cleanly, and to understand the cause of this error.





Aucun commentaire:

Enregistrer un commentaire