【问题标题】:Expression Does Not Produce a Value in Interface ICollection表达式不会在接口 ICollection 中产生值
【发布时间】:2015-02-04 18:04:13
【问题描述】:

我正在使用 Visual Studio 2013/.NET Framework 4.5 处理 Windows Presentation Foundation (WPF) 项目。这是在 VB.NET 中。

我正在尝试访问 TFS 数据,这是我以前从未做过的,所以我正在使用教程。然而,本教程是用 C# 编写的,所以我转换了代码。 C#代码编译运行没有错误,而VB代码没有。

C#:

public static class ExtensionsMethods
    {
        public static void AddOnUi<T>(this ICollection<T> collection, T item)
        {
            Action<T> addMethod = collection.Add;
            Application.Current.Dispatcher.BeginInvoke(addMethod, item);
        }
    }

VB:

Public Module ExtensionsMethods
        Sub New()
        End Sub
        <System.Runtime.CompilerServices.Extension> _
        Public Sub AddOnUi(Of T)(collection As ICollection(Of T), item As T)
            Dim addMethod As Action(Of T) = collection.Add                
            Application.Current.Dispatcher.BeginInvoke(addMethod, Item)
        End Sub
    End Module

编译器在第六行给出错误 - Argument not specified for parameter 'item' of 'Public Sub Add(item As T)'

如果我添加参数,像这样:

Dim addMethod As Action(Of T) = collection.Add(item)

我得到一个不同的错误 - 表达式不产生值

我不熟悉扩展方法。任何帮助将不胜感激。

【问题讨论】:

    标签: .net wpf vb.net


    【解决方案1】:

    Action(Of T) 是一个委托。 AddressOf 可用于创建一个。

    Public Module ExtensionsMethods
        Sub New()
        End Sub
        <System.Runtime.CompilerServices.Extension> _
        Public Sub AddOnUi(Of T)(collection As ICollection(Of T), item As T)
            Dim addMethod As Action(Of T) = AddressOf collection.Add
            Application.Current.Dispatcher.BeginInvoke(addMethod, item)
        End Sub
    End Module
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-11
      • 1970-01-01
      • 2014-04-17
      • 2021-05-13
      • 1970-01-01
      • 2011-04-06
      • 2016-05-30
      相关资源
      最近更新 更多