I have a backend class that has to raise an event with no arguments. The MethodIn开发者_如何学Govoker 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?
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.
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
精彩评论