使用LINQ将C#代码转换为VB

时间:2016-02-26 01:59:17

标签: c# vb.net linq

我正在尝试将下面的2个例程转换为VB,我遇到了免费的Telerik转换器问题。它通常工作得非常好。

例程是C#扩展例程,用于查找与递归地匹配特定类型的控件或控件的窗体控件:

   public static IEnumerable<T> FindAllChildrenByType<T>(this Control control)
   {
        var controls = control.Controls.Cast<Control>();
        var enumerable = controls as IList<Control> ?? controls.ToList();
        return enumerable
            .OfType<T>()
            .Concat<T>(enumerable.SelectMany(FindAllChildrenByType<T>));
    }

    public static T FindChildByType<T>(this Control control, String ctrlName)
    {
        foreach (var ctrl in from ctrl in FindAllChildrenByType<T>(control) let testControl = ctrl as Control where testControl != null && testControl.Name.ToUpperInvariant() == ctrlName.ToUpperInvariant() select ctrl)
        {
            return ctrl;
        }
        return default(T);
    }

我从Telerik网站获得的内容如下:

<System.Runtime.CompilerServices.Extension>
Public Function FindAllChildrenByType(Of T)(control As Control) As IEnumerable(Of T)
    Dim controls As Control() = control.Controls.Cast(Of Control)()
    Dim enumerable As IEnumerable = If(TryCast(controls, IList(Of Control)), controls.ToList())
    Return enumerable.OfType(Of T).Concat(enumerable.SelectMany(AddressOf FindAllChildrenByType(Of T)))
End Function

<System.Runtime.CompilerServices.Extension>
Public Function FindChildByType(Of T)(control As Control, ctrlName As String) As T
    For Each ctrl As T In From ctrl In FindAllChildrenByType(Of T)(control) Let testControl = TryCast(ctrl, Control) Where testControl IsNot Nothing AndAlso testControl.Name.ToUpperInvariant() = ctrlName.ToUpperInvariant() Select ctrl
        Return ctrl
    Next
    Return Nothing
End Function

这些例程在C#中使用时效果很好,我需要及时回顾一下在VB中使用这些例程。

非常感谢!

Don B。

1 个答案:

答案 0 :(得分:1)

转换有两个问题:

  1. 它没有使用Option Infer On - VB相当于&#39; var&#39;是离开类型并使用Option Infer On。这是第一次方法转换的唯一问题。转换器试图猜测这些并弄错了 - 没有必要猜测 - 让Option Infer这样做。
  2. 第二种方法使用&#c; ctrl&#39;两次在同一范围内 - 不知道为什么C#允许这样做。重命名&#39;外部&#39;对于每个变量。
  3. 更正的VB代码如下(注意扩展方法需要在&#39;模块中):

    Option Infer On
    
    Module Test
        <System.Runtime.CompilerServices.Extension>
        Public Function FindAllChildrenByType(Of T)(control As Control) As IEnumerable(Of T)
            Dim controls = control.Controls.Cast(Of Control)()
            Dim enumerable = If(TryCast(controls, IList(Of Control)), controls.ToList())
            Return enumerable.OfType(Of T).Concat(enumerable.SelectMany(AddressOf FindAllChildrenByType(Of T)))
        End Function
    
        <System.Runtime.CompilerServices.Extension>
        Public Function FindChildByType(Of T)(control As Control, ctrlName As String) As T
            For Each ctrlx In From ctrl In FindAllChildrenByType(Of T)(control) Let testControl = TryCast(ctrl, Control) Where testControl IsNot Nothing AndAlso testControl.Name.ToUpperInvariant() = ctrlName.ToUpperInvariant() Select ctrl
                Return ctrlx
            Next
            Return Nothing
        End Function
    End Module
    
相关问题