【发布时间】: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#