【问题标题】:Translate simple code with C#6 syntax to Vb.Net将具有 C#6 语法的简单代码转换为 Vb.Net
【发布时间】:2023-03-15 14:35:02
【问题描述】:

有人可以帮我把这个使用新 C# 6 语法的简单 sn-p 翻译成 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);
}

【问题讨论】:

  • Reflector v9 在将其反编译为 VB 方面做得非常好。只需获取您自己的副本即可。
  • 这可能有效。 Convert Code 或让您非常接近。

标签: c# .net vb.net syntax code-translation


【解决方案1】:

试一试:

<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)
  • 将 C# 类型检查替换为 VB TypeOf object Is type
  • 将 C# null 替换为 VB Nothing

【讨论】:

    【解决方案2】:

    包括 xml cmets,并更正另一个答案中的“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
    

    【讨论】:

      猜你喜欢
      • 2013-08-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-17
      • 2011-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多