这个代码转换失败的原因是什么?
What is the reason for this code conversion failure?
我正在使用 the telerik code converter 尝试将此 VB 代码转换为 C#:
''' <summary>
''' Return the name of a property, field, or local variable from a lambda expression.
''' </summary>
''' <typeparam name="T">Type of the property, field, or local variable.</typeparam>
''' <param name="expr">A lambda expression that refers to a property, field, or local variable, in the
''' form: '() => Class.Property' or '() => object.Property'.</param>
''' <returns>The name of the property represented by the provided lambda expression.</returns>
Friend Function GetMemberName(Of T)(expr As System.Linq.Expressions.Expression(Of Func(Of T))) As String
Dim memberExpr As System.Linq.Expressions.MemberExpression = TryCast(expr.Body, System.Linq.Expressions.MemberExpression)
If memberExpr Is Nothing Then _
Throw New ArgumentOutOfRangeException("The argument must be a lambda expression in the form: " &
"'() => Class.Member', '() => object.Member', or '() => fieldOrLocal'")
Const VBLocalPrefix = "$VB$Local_" 'odd prefix in $VB$ for local variable member names.
GetMemberName = memberExpr.Member.Name
If (GetMemberName.StartsWith(VBLocalPrefix)) Then GetMemberName = GetMemberName.Substring(VBLocalPrefix.Length)
End Function
我在输出窗格中收到此错误消息:
CONVERSION ERROR: Code could not be converted. Details:
-- line 8 col 8: invalid NonModuleDeclaration
Please check for any errors in the original code and try again.
我已经 Googled telerik "invalid NonModuleDeclaration" and "invalid NonModuleDeclaration" 仔细阅读了结果,但它们基本上都提供了解决方法(即回答方说“这是我为您手动完成的转换)并且没有解释是什么导致故障或如何避免它。
我知道我知道如何手动转换代码,但我的问题是:为什么转换器无法转换此代码?
我找到问题了!这是由于这一行:
Throw New ArgumentOutOfRangeException("The argument must be a lambda expression in the form: " &
"'() => Class.Member', '() => object.Member', or '() => fieldOrLocal'")
也许转换器不是最新的,但在 & 符号后添加下划线可以解决问题,现在代码已成功转换:
Throw New ArgumentOutOfRangeException("The argument must be a lambda expression in the form: " & _
"'() => Class.Member', '() => object.Member', or '() => fieldOrLocal'")
我正在使用 the telerik code converter 尝试将此 VB 代码转换为 C#:
''' <summary>
''' Return the name of a property, field, or local variable from a lambda expression.
''' </summary>
''' <typeparam name="T">Type of the property, field, or local variable.</typeparam>
''' <param name="expr">A lambda expression that refers to a property, field, or local variable, in the
''' form: '() => Class.Property' or '() => object.Property'.</param>
''' <returns>The name of the property represented by the provided lambda expression.</returns>
Friend Function GetMemberName(Of T)(expr As System.Linq.Expressions.Expression(Of Func(Of T))) As String
Dim memberExpr As System.Linq.Expressions.MemberExpression = TryCast(expr.Body, System.Linq.Expressions.MemberExpression)
If memberExpr Is Nothing Then _
Throw New ArgumentOutOfRangeException("The argument must be a lambda expression in the form: " &
"'() => Class.Member', '() => object.Member', or '() => fieldOrLocal'")
Const VBLocalPrefix = "$VB$Local_" 'odd prefix in $VB$ for local variable member names.
GetMemberName = memberExpr.Member.Name
If (GetMemberName.StartsWith(VBLocalPrefix)) Then GetMemberName = GetMemberName.Substring(VBLocalPrefix.Length)
End Function
我在输出窗格中收到此错误消息:
CONVERSION ERROR: Code could not be converted. Details:
-- line 8 col 8: invalid NonModuleDeclaration
Please check for any errors in the original code and try again.
我已经 Googled telerik "invalid NonModuleDeclaration" and "invalid NonModuleDeclaration" 仔细阅读了结果,但它们基本上都提供了解决方法(即回答方说“这是我为您手动完成的转换)并且没有解释是什么导致故障或如何避免它。
我知道我知道如何手动转换代码,但我的问题是:为什么转换器无法转换此代码?
我找到问题了!这是由于这一行:
Throw New ArgumentOutOfRangeException("The argument must be a lambda expression in the form: " &
"'() => Class.Member', '() => object.Member', or '() => fieldOrLocal'")
也许转换器不是最新的,但在 & 符号后添加下划线可以解决问题,现在代码已成功转换:
Throw New ArgumentOutOfRangeException("The argument must be a lambda expression in the form: " & _
"'() => Class.Member', '() => object.Member', or '() => fieldOrLocal'")