我有一个如下所示的类:
public class Product : IProduct
{
static private string _defaultName = "default";
private string _name;
private float _price;
/// Constructor
public Product()
{
_price = 10.0F;
}
public void ModifyPrice(float modifier)
{
_price = _price * modifier;
}
我希望ModifyPrice在特定值下什么也不做,但我同时也想调用将价格设置为10的构造函数。我尝试了以下代码:
var fake = new SProduct() { CallBase = true };
var mole = new MProduct(fake)
{
ModifyPriceSingle = (actual) =>
{
if (actual != 20.0f)
{
MolesContext.ExecuteWithoutMoles(() => fake.ModifyPrice(actual));
}
}
};
MProduct.Constructor = (@this) => (@this) = fake;
即使使用正确的构造函数对fake进行了良好的初始化,我也无法将其分配给@this。我还尝试了类似于以下的操作:
MProduct.Constructor = (@this) => { var mole = new MProduct(@this)... };
但是这一次我不能调用我的构造函数。我该怎么办?
AllInstances
类来实现你想要的功能。 - Gebb