2016-01-06 40 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