在派生的 C# 类中隐藏 VB 类的方法
我正在为一个名为 ImageEditor 的大型复杂类从一段遗留代码编写一组单元测试,其中图像处理和 GUI 功能没有严格划分。类中的方法之一 BaseImageChanged 似乎完全关心图像的显示方式,应该在我的单元测试中禁用以避免不必要的复杂性。测试项目是C#语言,原始代码是VB语言;我的想法是在 C# 中为该类创建一个装饰器,然后将该方法隐藏在一个不执行任何操作的空装饰器后面。然而,当我尝试运行单元测试时,VB 代码不断引用旧的 BaseImageChanged 方法,就好像替换方法不存在一样。这就是我正在做的事情:
(VB 类)
Public Class ImageEditor
...
Public Sub BaseImageChanged()
...
End Sub
...
End Class
(C# 类)
class ImageEditorTestingClass : ImageEditor_Accessor
{
public new void BaseImageChanged() {}
}
有没有办法实现这种互操作性,或者我必须找到另一种方法?
编辑:编译器错误结果是反射问题。此外,只要基类是 ImageEditor 的访问器而不是原始 ImageEditor 类,重写该方法就不起作用。(没有编译器错误,但基方法的行为没有被重写。)
I'm writing a set of unit tests for a large, complex class called ImageEditor from a piece of legacy code, where image processing and GUI functionality isn't strictly divided. One of the methods in the class, BaseImageChanged, seems to be concerned entirely with how images get displayed and should be disabled in my unit tests to avoid unnecessary complexity. The test project is in C#, and the original code is in VB; my idea was to create a Decorator for the class in C# and then hide the method behind an empty one that does nothing. However, when I try running the unit test, the VB code keeps referencing the old BaseImageChanged method as though the replacement didn't exist. Here's what I'm doing:
(VB class)
Public Class ImageEditor
...
Public Sub BaseImageChanged()
...
End Sub
...
End Class
(C# class)
class ImageEditorTestingClass : ImageEditor_Accessor
{
public new void BaseImageChanged() {}
}
Is there a way to accomplish this sort of interoperability, or will I have to find another way?
EDIT: The compiler error turned out to be a problem with reflection. Also, overriding the method didn't work as long as the base class was an accessor of ImageEditor instead of the original ImageEditor class.(No compiler error, but the base method's behavior wasn't overridden.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以访问 VB 代码吗?如果是这样,则将其标记为虚拟(C# 语法);然后在 C# 代码中用空主体覆盖它,以便 id 不执行任何操作。
您应该(几乎)永远不要使用“new”来重新声明方法或属性。如果某些代码假设它是基类,即使您传递派生类,也会调用基方法。
为了更好地理解,请阅读 .NET 中的后期绑定和早期绑定
早期和后期绑定
由于我下面的评论,@estanford 似乎接受了这个答案
“在您调用 BaseImageChanged 的地方尝试使用反射”
Do you have access to the VB code? if so mark it virtual (C# syntax); Then in the C# code override it with an empty body so that id does nothing.
You should (almost) never use 'new' to redeclare methods or properties. If some code is assuming it's the base class, even if you pass a derived one, the base methods will be called.
For better understanding read about late-binding and early-binding in .NET
Early and late binding
It seems @estanford accepted this answer due to my comment below
"Where you call BaseImageChanged try using reflection"
我根本不懂 VB,但这听起来更像是带有
覆盖
的虚拟
方法,而不是方法隐藏。 VB 代码可能在某处调用“this.BaseImageChanged()”,这不会调用您的新方法...对吧?I don't know VB at all, but this sounds more like a case for a
virtual
method with anoverride
than a method hiding. The VB code is probably calling "this.BaseImageChanged()" somewhere, which wouldn't call your new method... right?看起来这里发生的事情是
在这种情况下,唯一的选择你必须使用
new
。生成的访问器类不会添加虚拟方法,因此如果您想提供同名的替代方法,new
是唯一的方法。It looks like what's going on here is that
In this case the only option you have is to use
new
. The generated accessor class will not add virtual methods and hence if you want to provide an alternate method with the same namenew
is the only way to do so.