将C#语法的简单代码翻译成Vb.Net
本文关键字:翻译 Vb Net 代码 简单 语法 | 更新日期: 2023-09-27 18:29:08
有人可以帮我把这个使用新C#语法的简单片段翻译成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);
}
包括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
尝试一下:
<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
If(condition, true, false)
替换了C#三元运算符 - 用VB
TryCast(object, type)
替换了C#强制转换 - 用VB
TypeOf object Is type
代替C#类型检查 - 用VB
Nothing
代替C#null