Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GetProperty method is case sensitive for VB?

I was debugging one of the applications I work on and came across an interesting issue. For those unfamiliar with VB.NET, variable names are case insensitive. Therefore,

Dim foo As Integer
FOO = 5
System.Console.WriteLine(foO)

will compile and output a value of 5.

Anyways, the code I was working on was attempting to get a value from a property via reflection like so:

Dim value As Object = theObject.GetType().GetProperty("foo", BindingFlags.Public Or BindingFlags.Instance).GetValue(theObject, Nothing)

The code threw a NullReferenceException on this line. After doing some debugging, I noticed it is the GetProperty method that is returning Nothing, so GetValue is bombing. Looking at the class for theObject, it has a public property with the name "Foo", not "foo" (notice the difference in casing). Apparently, since I was searching for lowercase foo, it could not find the property at all via reflection.

Thinking this was some weird fluke, I created a quick and dirty sandbox app to test this finding:

Option Strict On
Option Explicit On

Imports System.Reflection

Module Module1

    Sub Main()
        Dim test As New TestClass
        Dim propNames As New List(Of String)(New String() {"testprop", "testProp", "Testprop", "TestProp"})

        'Sanity Check
        System.Console.WriteLine(test.testprop)
        System.Console.WriteLine(test.testProp)
        System.Console.WriteLine(test.Testprop)
        System.Console.WriteLine(test.TestProp)

        For Each propName As String In propNames
            Dim propInfo As PropertyInfo = test.GetType().GetProperty(propName, BindingFlags.Public Or BindingFlags.Instance)
            If propInfo Is Nothing Then
                System.Console.WriteLine("Uh oh! We don't have PropertyInfo for {0}", propName)
            Else
                System.Console.WriteLine("Alright, we have PropertyInfo for {0} and its value is: {1}", propName, propInfo.GetValue(test, Nothing))
            End If
        Next
    End Sub

    Public Class TestClass
        Private _testProp As String = "I got it!"
        Public Property TestProp() As String
            Get
                Return _testProp
            End Get
            Set(ByVal value As String)
                _testProp = value
            End Set
        End Property
    End Class

End Module

Much to my surprise, the output was as follows:

I got it!
I got it!
I got it!
I got it!
Uh oh! We don't have PropertyInfo for testprop
Uh oh! We don't have PropertyInfo for testProp
Uh oh! We don't have PropertyInfo for Testprop
Alright, we have PropertyInfo for TestProp and its value is: I got it!

TL;DR

Variable names in VB.NET are typically case insensitive. The GetProperty method on Type seems to be case sensitive when it comes to the property name. Is there a way to call this method "VB.NET style" and ignore case? Or am I SOL here and need to apply a C# mentality and worry about casing?

like image 826
avanek Avatar asked Nov 17 '11 18:11

avanek


1 Answers

Variable names in VB.NET are typically case insensitive.

That's a trick done by the compiler. The CLR itself is case-sensitive. Since reflection has nothing to do with the compiler, case-sensitivity applies.

Is there a way to call this method "VB.NET style" and ignore case?

Yes. Specify in your BindingFlags to IgnoreCase:

Dim propInfo As PropertyInfo = test.GetType().GetProperty(propName, BindingFlags.Public Or BindingFlags.Instance Or BindingFlags.IgnoreCase)
like image 123
vcsjones Avatar answered Sep 29 '22 12:09

vcsjones