您通常通过继承包含虚方法的类来覆盖虚方法,这没关系。 有没有办法在运行时以这样的方式绑定覆盖方法?
public class MyForm : Form
{
private AttachedClass attachedClass;
public MyForm()
{
InitializeComponent();
attachedClass = new AttachedClass(this);
}
}
internal class AttachedClass
{
private Form form;
public AttachedClass(Form form)
{
this.form = form;
// Bind AttachedClass.ProcessCmdKey() to form
.... ???
}
private bool ProcessCmdKey(ref Message msg, Keys keyData)
{
// Process keyData
....
form.ProcessCmdKey(ref msg, keyData);
}
}
将键处理到AttachedClass.ProcessCmdKey()中会很酷,就好像它写成:
public class MyForm : Form
{
public MyForm()
{
InitializeComponent();
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
// Process keyData
....
return base.ProcessCmdKey(ref msg, keyData);
}
}
我知道如何获取AttachedClass.ProcessCmdKey()的MethodInfo,或者最终如何使用Delegate.CreateDelegate()获取委托,但我没有找到将它绑定到AttachedClass构造函数内的表单实例的方法
有可能吗?
尼尔