反射获取出来的方法转成EventHandler
private void Form1_Load(object sender, EventArgs e)
{
//var intance = Activator.CreateInstance(typeof(Form1)) as Form1;
object target = this;
MethodInfo inf = typeof(TestClass).GetMethod("btnTest_Click", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
Delegate del = Delegate.CreateDelegate(typeof(EventHandler), target, inf);
EventHandler myMethod = del as EventHandler;
button1.Click += myMethod;
}
public class TestClass
{
public string GetValue(string value)
{
return value;
}
private void btnTest_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
Random r = new Random();
btn.Text = r.Next(1, 15).ToString();
}
}
报错 “System.ArgumentException”类型的未经处理的异常在 mscorlib.dll 中发生
其他信息: 无法绑定到目标方法,因其签名或安全透明度与委托类型的签名或安全透明度不兼容。