VB.net 匿名类型在 AJAX 调用中有不正确的 属性 大小写

VB.net anonymous type has incorrect property casing from AJAX call

我们注意到,有时 AJAX 调用控制器操作的结果表明 JSON 结果的大小写不正确。如果我们重建我们的解决方案并尝试完全相同的调用,返回的案例实际上会改变。在以下案例中,密钥的案例已经正确了一年多,直到现在它决定根据一些看似随机的情况开始随机更改。

如上图所示,JSON 结果的键是小写 "success"。然而,当我在 Chrome 的控制台中查看结果时,它是一个大写的 "Success"。这导致我们的 JavaScript 失败,因为它正在检查小写版本。

这是什么原因造成的?更重要的是,我们如何阻止它?

vb.net 不区分大小写,而 C# 区分大小写。这意味着编译器将只为以下每个匿名类型生成 one class(从第一个实例开始):

Dim a = New With {.success = True} 'Compiler generate a class based on this type
Dim b = New With {.Success = True} 'Same type as `a`
Dim c = New With {.sUcCeSs = True} 'Same type as `a`

Debug.WriteLine(a.GetType().Name)
Debug.WriteLine(b.GetType().Name)
Debug.WriteLine(c.GetType().Name)

VB$AnonymousType_0'1
VB$AnonymousType_0'1
VB$AnonymousType_0'1

以下是编译回 vb.net 时编译代码的样子:

<DebuggerDisplay("success={success}"), CompilerGenerated> _
Friend NotInheritable Class VB$AnonymousType_0(Of T0)
    ' Methods
    <DebuggerNonUserCode> _
    Public Sub New(ByVal success As T0)
        Me.$success = success
    End Sub

    <DebuggerNonUserCode> _
    Public Overrides Function ToString() As String
        Dim builder As New StringBuilder
        builder.Append("{ ")
        builder.AppendFormat("{0} = {1} ", "success", Me.$success)
        builder.Append("}")
        Return builder.ToString
    End Function

    Public Property success As T0
        <DebuggerNonUserCode> _
        Get
            Return Me.$success
        End Get
        <DebuggerNonUserCode> _
        Set(ByVal Value As T0)
            Me.$success = Value
        End Set
    End Property

    Private $success As T0

End Class