c# - 如何正确覆盖 ISupportInitialize 实现

标签 c# inheritance interface

我在 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/

相关文章:

java - 如何为 Java Swing 中的单击行为设置 JFileChooser?

c# - ASP.NET 与 Oracle 连接问题

c# - 使用 Oid/ASEncodeData 创建 PublicKey 抛出 CryptographyException

c# - 查找类是否继承接口(interface)的更有效方法?

c++ - 哪种类型的继承更可取?

java - 是否可以在 Java 中为具有接口(interface)成员变量的类编写复制构造函数?

c# - 如何在 Microsoft Visual C# 2010 Express 中部署项目?

c# - 使用 DataContractSerialize 保存文件期间发生序列化错误

PHP 命名空间覆盖 Use 语句

德尔福Rtti : Explore properties of interfaces?