代码之家  ›  专栏  ›  技术社区  ›  Luca Matteis

C#回调不起作用

  •  1
  • Luca Matteis  · 技术社区  · 14 年前

    我正在定义一个将另一个函数作为参数的函数(使用Microsoft(R)Visual C#2005编译器版本8.00.50727.4927),但出现了一个奇怪的错误。

    函数定义如下:

    public ManagementScope ConnectComputerWMI(string computerName, 
        string username, string password, 
        Action callbackProcessEnd) {... }
    

    error CS0305: Using the generic type 'System.Action<T>' requires '1' type arguments
    

    我不知道是什么类型的系统动作需要。

    1 回复  |  直到 14 年前
        1
  •  6
  •   Jon Skeet    14 年前

    错误消息使它看起来好像不知道非泛型 Action 委托,我能想到的唯一原因就是你的目标是.NET2.0。仅在.NET 2.0中 Action<T> 存在;.NET3.5中引入了更多的版本,在.NET4中更是如此。

    编辑:我刚刚看到您正在使用VisualStudio2005—这意味着您 以.NET 2.0为目标。(您是否有指定.NET 4的问题的早期版本?我可以发誓你做到了……)

    public delegate void Action();
    

    能够 MethodInvoker 或者(它有一个等价的签名,但不幸的是在Windows窗体名称空间中-如果不使用Windows窗体,则不理想)。。。

    或者您可以升级到更现代的visualstudio和.NET版本。