When writing delegate, I encountered a problem. On the basis of an existing delegate without parameters, I tried to add another delegate with parameters, result VS reports the error "already contains a definition for 'invokedelegate.
At first glance, the Code seems to be okay:
InvokeDelegate( param);
In fact, this is equivalent to the delegate method, so we will think that the method overload method can also be used to reload the delegate.
After reading the book CLR via C #, or directly using the ILDASM tool, you will understand that the delegate will be translated into a class that inherits MulticastDelegate during compilation.
Therefore, the actual delegate code should be like this:
InvokeDelegate(Object IAsyncResult BeginInvoke(AsyncCallback callback, Object InvokeDelegate(Object IAsyncResult BeginInvoke( param, AsyncCallback callback, Object }
This makes it easy to see that the previous writing method will generate two classes with the same name, which of course is not allowed. The only difference between the two lies in the parameters in the BeginInvoke method.
Is there any way to reload delegate?
Let's take a look at the existing Action delegation in. NET Framework. Action, Action, Action ...... There are various forms. Let's take a look at their syntax.
Action delegate: public delegate void Action ()
Action delegate: public delegate void Action (T obj)
Action delegate: public delegate void Action (T1 arg1, T2 arg2)
Obviously, the wildcard mode can be used to meet the need to overload the delegate.
If you change "private delegate void InvokeDelegate (string param);" to "private delegate void InvokeDelegate <in T> (T param);", the following new classes are generated.
InvokeDelegate<T> InvokeDelegate(Object IAsyncResult BeginInvoke(T param, AsyncCallback callback, Object }
This method will not conflict with the first delegate.
However, it should be added that the statement of reload delegate is not correct. In fact, it only defines different types of delegate, rather than overload operations.
Published in my blog at the same time