0
例如:爲什麼.NET模擬框架不能使用new來隱藏非密封類的非虛方法?
public class ThirdPartyClass
{
public void DoSomething() { ... }
}
// Mock framework generated class
public class MockThirdPartyClass : ThirdPartyClass
{
public new void DoSomething() { // Mock user's deletegate goes here }
}
我懷疑的問題是,測試中的類使用它的變量/參數的基類,因此調用嘲笑類的方法去的基礎版本,而不是陰影版本:
public class MyClass
{
private ThirdPartyClass tpc;
public MyClass() { }
public MyClass(ThirdPartyClass tpc)
{
this.tpc = tpc;
}
public void MyClassDoesSomething()
{
this.tpc.DoSomething(); // Bypasses MockThirdPartyClass shadow method
}
這是正確的嗎?