tags:

views:

95

answers:

1

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.

+6  A: 

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.

Jon Skeet
+1 - you are probably right: *"using Microsoft (R) Visual C# 2005 Compiler"*. Side note: `System.Action` was introduced in .NET 3.5.
Fredrik Mörk
@Fredrik: I think that was added later... I could have sworn it initially said .NET 4.
Jon Skeet
@Jon: it's present in the 3.5 documentation: http://msdn.microsoft.com/en-us/library/system.action(v=VS.90).aspx
Fredrik Mörk
@Fredrik: Yes, it was introduced in .NET 3.5. My point is that the *question* originally claimed he was using .NET 4, I believe.
Jon Skeet
Action<T> was introduced in 2.0. But Action in 3.5.
Jérémie Bertrand
@Jon: Ah, now I get it. Makes sense now (even to me) :)
Fredrik Mörk
You're right, I switched to version 4.0 thanks!
Luca Matteis