【问题标题】:What is the reason for this code conversion failure?这个代码转换失败的原因是什么?
【发布时间】:2016-09-24 19:15:27
【问题描述】:

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

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

转换错误:无法转换代码。详情:

-- line 8 col 8: 无效的 NonModuleDeclaration

请检查原始代码中是否有任何错误,然后重试。

我已经 Googled telerik "invalid NonModuleDeclaration""invalid NonModuleDeclaration" 仔细阅读了结果,但它们基本上都给出了解决方法(即答复方说“这是我为你手动完成的转换)并且没有解释什么是导致失败或如何避免失败。

我知道我可以弄清楚如何手动转换代码,但我的问题是:为什么转换器无法转换此代码?

【问题讨论】:

  • 我在第 8 行第 8 列得到错误 - 如果我没记错的话,这部分是:System.Linq.Expressions.Expression(Of Func(Of T))
  • 我的错,我删除了 XML 文档以尝试查看是否是原因,并且代码也有缩进 - 我从中复制了输出。当我把它放回去(更新)时,我得到了相同的行/列。

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


【解决方案1】:

我发现了问题!正是由于这一行:

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

【讨论】:

  • @roryap :没问题!很高兴我能帮上忙。
猜你喜欢
  • 1970-01-01
  • 2019-07-08
  • 1970-01-01
  • 2021-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多