3

当将以下行从 C# 转换为 VB.Net 时,我得到

表达式不产生值

C#

query.ToList().ForEach(ti => cat.Add(ti));

VB.NET

query.ToList().ForEach(Function(ti) cat.Add(ti))

C#代码:

void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
    new DesignerMetadata().Register();

    var toolbox = new ToolboxControl();
    var cat = new ToolboxCategory("Standard Activities");
    var assemblies = new List<Assembly>();
    assemblies.Add(typeof(Send).Assembly);
    assemblies.Add(typeof(Delay).Assembly);
    assemblies.Add(typeof(ReceiveAndSendReplyFactory).Assembly);

    var query = from asm in assemblies
                from type in asm.GetTypes()
                where type.IsPublic &&
                !type.IsNested &&
                !type.IsAbstract &&
                !type.ContainsGenericParameters &&
                (typeof(Activity).IsAssignableFrom(type) ||
                typeof(IActivityTemplateFactory).IsAssignableFrom(type))
                orderby type.Name
                select new ToolboxItemWrapper(type);

    query.ToList().ForEach(ti => cat.Add(ti));
    toolbox.Categories.Add(cat);
    Grid.SetColumn(toolbox, 0);
    Grid.SetRow(toolbox, 1);
    LayoutGrid.Children.Add(toolbox);
}

我想要 Vb.net 转换。当我在 vb.net 中转换此代码时,在 query.ToList().ForEach(Function(ti) cat.Add(ti)) 中出现错误,此行 .error 是 Expression 不会产生值。

转换后的 VB.NET 代码

  Private Sub MainWindow_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
    Dim metadata = New DesignerMetadata()
    metadata.Register()

    'Create the ToolBoxControl
    Dim toolbox = New ToolboxControl()

    'Create a collection of category items
    Dim cat = New ToolboxCategory("Standard Activities")
    Dim assemblies = New List(Of Assembly)()
    assemblies.Add(GetType(SendAndReceiveReplyFactory).Assembly)
    assemblies.Add(GetType(Delay).Assembly)
    assemblies.Add(GetType(ReceiveAndSendReplyFactory).Assembly)

    Dim query = _
     From asm In assemblies
     From type In asm.GetTypes() _
     Where type.IsPublic AndAlso Not type.IsNested AndAlso Not type.IsAbstract AndAlso Not type.ContainsGenericParameters AndAlso (GetType(Activity).IsAssignableFrom(type) OrElse GetType(IActivityTemplateFactory).IsAssignableFrom(type)) _
     Order By type.Name
     Select New ToolboxItemWrapper(type)

    query.ToList().ForEach(Function(ti) cat.Add(ti))
    toolbox.Categories.Add(cat)
    Grid.SetColumn(toolbox, 0)
    Grid.SetRow(toolbox, 1)
    LayoutGrid.Children.Add(toolbox)
End Sub
4

1 回答 1

13

将有问题的行更改为该行:

query.ToList().ForEach(Sub(ti) cat.Add(ti)) 

这是必要的,因为Action<T>( Sub(T)) 和Func<T>( Function(t)) 在 VB.NET 中有单独的语法(但在 C# 中没有)。

于 2013-07-18T06:29:50.610 回答