How to reload delegate

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.