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

标签 c# vb.net telerik code-conversion vb.net-to-c#

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

相关文章:

c# - 列中带有按钮的 .net Radgrid 仅在第二次单击时触发 itemcommand

sql-server - 设置为Null但不设置为0-int

c# - 在 .NET App 中获取 IIS 应用程序名称

c# - 查找可用时间段 C#

vb.net - Excel 保存在 C :\\MyDocuments Defaultly

c# - 如何用具有相同文本的旧值替换字符串两次?

c# - 如何在oxyplot中绘制经验分布函数?

c# - 共享成员(const)的类的静态方法将是线程安全的吗?

vb.net - 将参数用于 Oracle ODBC 连接

asp.net-core - asp.net 核心 Begin Collection Item 核心不适用于剑道控件