gpt4 book ai didi

c# - 这个代码转换失败的原因是什么?

转载 作者:行者123 更新时间:2023-11-30 15:21:31 24 4
gpt4 key购买 nike

我正在使用 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

我在输出 Pane 中收到此错误消息:

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""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'")

关于c# - 这个代码转换失败的原因是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37466690/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com