将使用 C#6 语法的简单代码翻译成 Vb.Net

Translate simple code with C#6 syntax to Vb.Net

有人可以帮助我将这个使用新的 C# 6 语法的简单片段翻译成 Vb.Net?

在某些在线服务(例如 Telerik.

中进行翻译时,生成的 LINQ 查询不完整(语法错误)
/// <summary>
///     An XElement extension method that removes all namespaces described by @this.
/// </summary>
/// <param name="this">The @this to act on.</param>
/// <returns>An XElement.</returns>
public static XElement RemoveAllNamespaces(this XElement @this)
{
    return new XElement(@this.Name.LocalName,
        (from n in @this.Nodes()
            select ((n is XElement) ? RemoveAllNamespaces(n as XElement) : n)),
        (@this.HasAttributes) ? (from a in @this.Attributes() select a) : null);
}

试一试:

<System.Runtime.CompilerServices.Extension()> _
Public Function RemoveAllNamespaces(this As XElement) As XElement
    Return New XElement(this.Name.LocalName,
        (From n In this.Nodes
         Select (If(TypeOf n Is XElement, TryCast(n, XElement).RemoveAllNamespaces(), n))),
        If((this.HasAttributes), (From a In this.Attributes Select a), Nothing))
End Function

如果您要转换其他代码,这是我采取的步骤:

  • 添加了 Extension 属性,因为 VB 没有像 C# 那样的用于创建扩展方法的语法。
  • 将 C# 三元运算符替换为 VB If(condition, true, false)
  • 将 C# 强制转换替换为 VB TryCast(object, type)
  • 用 VB TypeOf object Is type.
  • 替换了 C# 类型检查
  • 将 C# null 替换为 VB Nothing

包括 xml 评论,并更正另一个答案中的 'RemoveAllNamespaces' 错误,您的 VB 等价物是:

''' <summary>
'''     An XElement extension method that removes all namespaces described by @this.
''' </summary>
''' <param name="this">The @this to act on.</param>
''' <returns>An XElement.</returns>
<System.Runtime.CompilerServices.Extension> _
Public Function RemoveAllNamespaces(ByVal this As XElement) As XElement
    Return New XElement(this.Name.LocalName, (
        From n In this.Nodes()
        Select (If(TypeOf n Is XElement, RemoveAllNamespaces(TryCast(n, XElement)), n))),If(this.HasAttributes, (
            From a In this.Attributes()
            Select a), Nothing))
End Function