简体   繁体   中英

C# callbacks not working

I'm defining a function that takes another function as a parameter (using Microsoft (R) Visual C# 2005 Compiler version 8.00.50727.4927) but I get a weird error.

Here's the function definition:

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

And here's the error:

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

I'm not sure what type System.Action needs.

The error message makes it look like it doesn't know about the non-generic Action delegate, and the only reason I can think of for that is that you're targeting .NET 2.0. In .NET 2.0, only Action<T> existed; more versions were introduced in .NET 3.5, and even more in .NET 4.

EDIT: I've just seen that you're using Visual Studio 2005 - which means you are targeting .NET 2.0. (Did you have a previous version of the question which specified .NET 4? I could have sworn you did...)

That's the problem... now there are various solutions. You could declare your own delegate:

public delegate void Action();

Or you could use MethodInvoker or (which has an equivalent signature, but is unfortunately in the Windows Forms namespace - not ideal if you're not using Windows Forms)...

Or you could upgrade to a more modern version of Visual Studio and .NET.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM