【问题标题】:GetProperty method is case sensitive for VB?GetProperty 方法对 VB 是否区分大小写?
【发布时间】:2011-11-17 18:47:42
【问题描述】:

我正在调试我正在处理的一个应用程序,遇到了一个有趣的问题。对于那些不熟悉 VB.NET 的人来说,变量名不区分大小写。因此,

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

将编译并输出值 5。

无论如何,我正在编写的代码试图通过反射从属性中获取值,如下所示:

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

代码在这一行引发了 NullReferenceException。做了一些调试后,我注意到返回 Nothing 的是 GetProperty 方法,所以 GetValue 正在轰炸。查看 theObject 的类,它有一个名为 "Foo" 的公共属性,而不是 "foo"(注意大小写的不同)。显然,由于我正在搜索小写的 foo,所以它根本无法通过反射找到该属性。

认为这是一些奇怪的侥幸,我创建了一个快速而肮脏的沙盒应用程序来测试这一发现:

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

令我惊讶的是,输出如下:

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

VB.NET 中的变量名通常不区分大小写。当涉及到属性名称时,Type 上的 GetProperty 方法似乎区分大小写。有没有办法将此方法称为“VB.NET 样式”并忽略大小写?还是我这里是 SOL 并且需要应用 C# 的心态并担心大小写?

【问题讨论】:

  • 你需要应用C#的心态,担心大小写。
  • GetProperty().Net,而不是 VB。那是你的问题。
  • 是的,我很清楚这是 .NET 与 VB 的对比。我实际上是从一个 C# 人开始的,并参与了这个主要是 VB 的项目。我不会在 C# 中考虑这个场景,但发现它在 VB 中有点令人困惑。
  • 我的观点是 .Net 类在从 VB 或 C# 调用时的行为并没有什么不同。
  • 对,可以理解。虽然对于只使用 VB 编码的人来说可能并不直观(我在这艘船上与很多人一起工作)。正如 GetProperty 对于来自 C# 思维模式的人实际上不区分大小写可能是违反直觉的。归根结底,这都是 .NET 和 CLR,但我通常在思考语言而不是框架时编写代码 :)

标签: vb.net reflection case-sensitive


【解决方案1】:

VB.NET 中的变量名通常不区分大小写。

这是编译器完成的一个技巧。 CLR 本身区分大小写。由于反射与编译器无关,因此区分大小写。

有没有办法将此方法称为“VB.NET 样式”并忽略大小写?

是的。在您的 BindingFlags 中指定 IgnoreCase:

Dim propInfo As PropertyInfo = test.GetType().GetProperty(propName, BindingFlags.Public Or BindingFlags.Instance Or BindingFlags.IgnoreCase)

【讨论】:

  • 哇,不知道我怎么错过了 BindingFlag.IgnoreCase 枚举。感谢您的快速回复!
猜你喜欢
  • 2018-10-15
  • 1970-01-01
  • 2011-08-15
  • 2010-09-14
  • 2017-07-13
  • 2011-11-16
  • 1970-01-01
  • 2011-01-19
  • 2013-03-16
相关资源
最近更新 更多