这段代码转换失败的原因是什么?

时间:2016-05-26 16:43:38

标签: 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

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

  

CONVERSION ERROR:代码无法转换。详细说明:

     

- 第8行第8栏:无效的NonModuleDeclaration

     

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

Googled telerik "invalid NonModuleDeclaration""invalid NonModuleDeclaration"并仔细阅读了结果,但它们基本上都提供了解决方法(即应答方说&#34;这是我手动执行的转换你不知道导致失败的原因或如何避免失败。

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

1 个答案:

答案 0 :(得分:3)

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

h(1) = subplot(3,1,1,'Position',[.1 .75 .2 .2])

也许转换器不是最新的,但在&符号后添加下划线可以解决问题并且代码现在已成功转换:

Throw New ArgumentOutOfRangeException("The argument must be a lambda expression in the form: " &
    "'() => Class.Member', '() => object.Member', or '() => fieldOrLocal'")