如何正确重写 ISupportInitialize 实现
我陷入了 ISupportInitialize
的困境。
我们使用继承自 System.Windows.Form.BindingSource 的自定义类。 现在,我们需要从继承的类中增强 ISupportInitialize 实现,以自动检查表单上的控件/组件,因为应尽可能减少手动工作。
问题是,该接口是从微软显式实现的,因此我无法调用基类的 BeginInit()
和 EndInit()
方法,也无法覆盖它。
仅实现新方法就会阻止基类正常工作,因为这些方法不会被调用,不是吗?
任何提示表示赞赏...
I'm stuck at a point with ISupportInitialize
.
We use a custom class inherited from System.Windows.Form.BindingSource.
Now we need to enhance the ISupportInitialize
implementation from our inherited class to check for controls/components on the form automatically, since manual work should be minimized where possible.
The problem is, that the interface is explicitly implemented from microsoft and so i cannot call the BeginInit()
and EndInit()
Methods of the base class nor override it.
Just implementing new methods would stop the base class from working as usual since the methods will not get called, will they?
any hint appreciated...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个非常有趣的问题!
在我看来,调用基类中显式实现的方法的唯一方法是使用反射。像这样的东西应该可以完成工作(未经测试):
This is a pretty interesting question!
In my opinion the only way to call the explicitely implemented methods in the base class is to use reflection. Something like this should do the job (untested):
您无法覆盖它,并且使用反射器查看源代码告诉我您将无法在这里做太多事情...您可以尝试对所有接口使用装饰器模式,但无论哪种方式您都可能会陷入困境,因为此类由框架使用,并且您无法控制它的用法。
无论如何,如果您想尝试一下,想法如下:
如下所示:
同样,这将依赖所有客户端代码(也在框架中)来遍历接口,这是我不会花钱的。
You can not override it, and looking at the source with reflector tells me you won't be able to do much here... You could try to use the decorator pattern for all the interfaces, but chances are you will get stuck either way, since this class is used by the framework and you don't control it's usage.
Anyway, if you want to try it, here's the idea:
Something like this:
Again, this would rely on all client code (also in the framework) to go through the interfaces, something I would not put money on.
我使用几个扩展方法来实现此目的:
在您的类中,显式实现 ISupportInitialize 并调用适当的扩展方法,例如:
您不必显式实现这两个方法,因为基类已经实现了它,因此如果您只想在初始化后添加逻辑,则可以省略
BeginInit()
。I use a couple of extension methods to achieve this:
In your class, explicitly implement
ISupportInitialize
and call the appropriate extension method, for example:You don't have to explicitly implement both methods, because the base class already implements it, so if you're only interested in adding logic after initialisation, you can leave out
BeginInit()
.