1

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.

Derek TP
  • 13
  • 1
  • 2
  • Which .NET Framework are you targetting? And is that the full Framework or the Client Profile? – Visual Vincent May 05 '16 at 21:27
  • Framework 4.0 (would be using a later version but constrained by the host where the site will run). Full framework. – Derek TP May 05 '16 at 22:13
  • Does sound strange. Will try it myself in a moment. I'm using VS 2010 so because of that it's good that you use .NET 4.0. ;) – Visual Vincent May 05 '16 at 22:18
  • Possibly answering my own question... If I change the line in question to `For Each x In prop.GetCustomAttributes(False)` it compiles fine. Is this because although the project is targetting 4.0, I do have 4.5 installed on the machine? ... and it seems the CustomAttributes property came in with 4.5, whilst the GetCustomAttributes function pre-dates (pre-versions!) it. – Derek TP May 05 '16 at 22:25
  • Well that seems to be correct. Visual Studio apparently displays all possible classes for all Frameworks (can be changed in it's "Browse" menu). As I said I use VS 2010 which don't go any further than .NET 4.0, and I don't have the `CustomAttributes` property. The `GetCustomAttributes()` method is the older version of it. – Visual Vincent May 05 '16 at 22:45

0 Answers0