Http://www.cnblogs.com/xiaofei59/archive/2010/11/25/1887285.html#]
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.
Check the following 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 );