我有这样的课:
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)... };
但是这次我不能调用我的构造函数。我该怎么办?