2016-01-06 41 views
0

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

产生的LINQ查询是不完整的(错误的语法)从Telerik做这样一个在某些在线服务转换时。

/// <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); 
} 
+0

反射V9的表现非常体面的工作,反编译这VB。拿到你自己的副本。 –

+0

这可能工作。 [转换代码](http://converter.telerik.com/)或让你非常接近。 – JAZ

回答

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#那样的语法来创建扩展方法。使用VB替换C#三元运算符If(condition, true, false)
  • 用VB代替C#cast。
  • 用VB替换C#类型检查TypeOf object Is type。使用VB Nothing代替C#null
2

包括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