我在 ISupportInitialize
上卡住了。
我们使用继承自 System.Windows.Form.BindingSource 的自定义类。
现在我们需要增强继承类的 ISupportInitialize
实现,以自动检查表单上的控件/组件,因为应尽可能减少手动工作。
问题是,该接口(interface)是从 Microsoft 显式实现的,因此我无法调用基类的 BeginInit()
和 EndInit()
方法,也无法覆盖它.
仅仅实现新方法会阻止基类照常工作,因为这些方法不会被调用,是吗?
感谢任何提示...
最佳答案
这是一个非常有趣的问题!
在我看来,调用基类中明确实现的方法的唯一方法是使用反射。像这样的东西应该可以完成工作(未经测试):
public class YourBindingSource : BindingSource, ISupportInitialize
{
public void BeginInit()
{
Type baseType = base.GetType();
InterfaceMapping interfaceMapping = baseType.GetInterfaceMap(typeof(ISupportInitialize));
foreach(MethodInfo targetMethod in interfaceMapping.TargetMethods)
{
bool isBeginInitMethod = ...; // Could be determined by checking the name..
if(isBeginInitMethod)
{
targetMethod.Invoke(this, new object[0]);
}
}
}
}
关于c# - 如何正确覆盖 ISupportInitialize 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5895951/