Exception {Unable to convert anonymous method to type ' System.Delegate ' because it is not a delegate type}
The delegate actually takes the method name as a parameter, but if there are many methods, it is necessary to indicate which parameter
See the following code:
This. Invoke (delegate
{
MessageBox.Show ("T4");
});
Developers familiar with WinForm know that this is an instance of a form, so do not explain otherwise. Running the code causes an exception: {The anonymous method cannot be converted to type ' System.Delegate ' because it is not a delegate type}.
In fact, from the wrong information to see, this anonymous method is written in a little bit of a problem. The key to the problem is to invoke the parameters of this function, and we look at its prototype as:
public Object Invoke (Delegate method)
It is also said that it accepts a delegate, so any instance derived from delegate is acceptable. We know that similar threadstart,methodinvoker are derived from delegate, then the compiler in the conversion of this anonymous function, do not know whether to convert this anonymous function ThreadStart or methodinvoker, so error. (Representing a delegate function, as well as Parameterizedthreadstart, WaitCallback, AsyncCallback, and so on, except that 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 the anonymous function to.
Off topic: Note here, whether new or transformed, is the same.
Now summarize the following types of grammar:
private void Button1_Click (object sender, EventArgs e)
{
Convert delegate to ThreadStart
thread T1 = new Thread ((ThreadStart) delegate {MessageBox.Show ("T1");});
T1. Start ();
The second way to convert delegate to ThreadStart
Thread t2 = new Thread (new ThreadStart (delegate () {MessageBox.Show ("T2");}));
T2. Start ();
Convert delegate to MethodInvoker
This. Invoke (new MethodInvoker (Delegate {MessageBox.Show ("T3");});
Convert delegate to ThreadStart
This. Invoke ((ThreadStart) delegate
{
MessageBox.Show ("T4");
});
Convert delegate to WaitCallback
ThreadPool.QueueUserWorkItem ((WaitCallback) delegate
{
MessageBox.Show ("T5");
});
By default, delegate is converted to WaitCallback because QueueUserWorkItem only accepts WaitCallback parameters
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, a few delegate prototypes are attached:
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);
About delegates: Exception {Unable to convert anonymous method to type ' System.Delegate ' because it is not a delegate type}