我正在尝试将下面的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。
答案 0 :(得分:1)
转换有两个问题:
更正的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