views:

180

answers:

2

I have a backend class that has to raise an event with no arguments. The MethodInvoker has the desired signature but it feels a little odd to use it in a backend class since it is defined in System.Windows.Forms. Is there another delegate with this signature in other namespaces in the .Net Framework? If not, do you think that I should define a delegate with that signature myself or use MethodInvoker instead?

+1  A: 

If you can use .NET 3.5 there's the Action delegate. If not, then you can always declare that (+ any other Action or Func delegates you want) in a Utils class or similar:

public delegate void Action();
public delegate void Action<T1, T2>(T1 arg0, T2 arg1);

public delegate TResult Func<T1, TResult>(T1 arg0);
public delegate TResult Func<T1, T2, TResult(T1 arg0, T2 arg1);

etc...

Note that .NET 2 already includes an void Action<T>(T arg) delegate, but doesn't have any of the others

thecoop
Unfortunately, I am using .NET Framework 2.0. What do you think generally about the usage of MethodInvoker in a backend class?
Ikaso
I would create your own versions of the .NET 3.5 delegates in a common namespace; that way it's easy to convert to 3.5 when you do do so. And MethodInvoker is explicitly designed for winforms cross-thread invokes; to use it as a general method seems conceptually wrong.
thecoop
+1  A: 

If you are using .NET 2.0:
Define your own delegate type. Referencing an assembly only for such a simple type is not good idea in my opinion.

Many frameworks do so, too. Even in mscorlib (2.0) there might be some of them. However, they might be in deeper namespaces whose usage might be puzzling in combination with your class.

Therefore, use your own delegate type outside .NET 3.5.

winSharp93