having a function parameter of type delegate. the function adds an eventhandler and expects the method as parameter. how to achieve this?
example:
private void myFunction( Delegate del )
{
...
var b = new Button();
b.Click += new EventHand开发者_如何学运维ler( <method delegate refers to should be used> );
}
If you can change the type of del
to EventHandler
then you can just use the following:
b.Click += del;
At the moment, no that doesn't make sense - because it could be a delegate of any type.
What would you want to happen if the delegate passed in took 15 parameters? How would it be called?
If you could give more information about what you're trying to achieve - and why you're trying to use just Delegate
instead of some specific delegate type - that would help.
The parameter to myFunction should be an Action<object, EventArgs>
private void myFunction(Action<object, EventArgs> del)
{
var b = new Button();
b.Click += new EventHandler(del);
}
Or just an EventHandler
Try to use such code:
...............
private void btn_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello");
}
private void MyFunc(EventHandler Meth)
{
button1.Click += Meth;
}
private void TestCall()
{
MyFunc(btn_Click);
}
............
精彩评论