About delegation: exception {the anonymous method cannot be converted to the type "system. Delegate" because it is not the delegate type}

Source: Internet
Author: User

Exception {the anonymous method cannot be converted to the type "system. Delegate" because it is not the delegate type}

The delegate actually uses the method name as a parameter. However, if there are multiple methods, you must specify the parameter.

View the following information:Code:
This. Invoke (delegate
{
MessageBox. Show ("T4 ");
});
Developers familiar with winform know that this is an instance of a form, so there is no additional explanation. If the code is run, an exception occurs: {the anonymous method cannot be converted to the type "system. Delegate" because it is not the delegate type }.

In fact, from the error message, this anonymous method does not have any problems. The key to the problem is the parameter of the invoke function. The prototype is as follows:
Public object invoke (delegate method)
That is to say, it accepts a delegate. Therefore, any instance derived from delegate is acceptable. We know that threadstart and methodinvoker are derived from delegate, so when the compiler converts this anonymous function, it does not know whether to convert this anonymous function to threadstart or methodinvoker, so an error is reported. (Representing a delegate function, there are parameterizedthreadstart, waitcallback, asynccallback, etc., but they all have parameters .)
The correct syntax should be as follows:
This. Invoke (New methodinvoker (delegate {MessageBox. Show ("T3 ");}));
Or
This. Invoke (threadstart) Delegate
{
MessageBox. Show ("T4 ");
});
In this way, the compiler knows which parameter to convert an anonymous function.
Digress: note that both new and transformation are the same here.

Various syntaxes are summarized as follows:
Private void button#click (Object sender, eventargs E)
{
// Convert delegate into threadstart
Thread T1 = new thread (threadstart) delegate {MessageBox. Show ("T1 ");});
T1.start ();
// The second method for converting delegate into threadstart
Thread t2 = new thread (New threadstart (delegate () {MessageBox. Show ("T2 ");}));
T2.start ();
// Convert delegate into methodinvoker
This. Invoke (New methodinvoker (delegate {MessageBox. Show ("T3 ");}));
// Convert delegate into threadstart
This. Invoke (threadstart) Delegate
{
MessageBox. Show ("T4 ");
});
// Convert delegate into waitcallback
Threadpool. queueuserworkitem (waitcallback) Delegate
{
MessageBox. Show ("T5 ");
});
// By default, delegate is converted to waitcallback, because queueuserworkitem only accepts the waitcallback Parameter
Threadpool. queueuserworkitem (delegate
{
MessageBox. Show ("T5 ");
});
Waitcallback WC = new waitcallback (this. dosomethingwithstate );
Threadpool. queueuserworkitem (WC, "I am state .");
}

Void dosomethingwithstate (Object C)
{
MessageBox. Show ("T6" + C. tostring ());
}

Finally, we have attached several delegate prototypes:
Public Delegate void threadstart ();
Public Delegate void methodinvoker ();
Public Delegate void waitcallback (Object State );
Public Delegate void parameterizedthreadstart (Object OBJ );
Public Delegate void asynccallback (iasyncresult AR );

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.