Domanda

Ho una classe come questa:

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;
    }  
.

Voglio ModifyPrice per non fare nulla per un valore specifico, ma voglio anche chiamare il costruttore che fissa il prezzo su 10. Ho provato qualcosa del genere:

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;
.

Ma anche se falso è ben inizializzato con il buon costruttore, non posso assegnarlo a @Questo.Provo anche qualcosa come

MProduct.Constructor = (@this) => { var mole = new MProduct(@this)... };
.

Ma questa volta non posso chiamare il mio costruttore.Come dovrei fare?

È stato utile?

Soluzione

Non è necessario scegliere il costruttore, il costruttore senza parametri della classe Product ha già ciò che desideri.

Aggiungi un po 'di debug di output su Product.

public class Product
{
    private float _price;
    public Product()
    {
        _price = 10.0F;
        Debug.WriteLine("Initializing price: {0}", _price);
    }
    public void ModifyPrice(float modifier)
    {
        _price = _price*modifier;
        Debug.WriteLine("New price: {0}", _price);
    }
}
.

derisione solo il metodo ModifyPrice.

[TestMethod]
[HostType("Moles")]
public void Test1()
{
    // Call a constructor that sets the price to 10.
    var fake = new SProduct { CallBase = true };
    var mole = new MProduct(fake)
    {
        ModifyPriceSingle = actual =>
        {
            if (actual != 20.0f)
            {
                MolesContext.ExecuteWithoutMoles(() => fake.ModifyPrice(actual));
            }
            else
            {
                Debug.WriteLine("Skipped setting price.");
            }
        }
    };
    fake.ModifyPrice(20f);
    fake.ModifyPrice(21f);
}
.

Vedere l'output di debug per confermare tutto funziona come previsto:

    Initializing price: 10
    Skipped setting price.
    New price: 210
.

A proposito, non è necessario utilizzare lo stub qui,

var fake = new SProduct { CallBase = true };
.

La creazione di un'istanza di Product sarà sufficiente.

var fake = new Product();
.

Aggiornamento: Dering un singolo metodo può essere ottenuto con la classe AllInstances come questa

MProduct.Behavior = MoleBehaviors.Fallthrough;
MProduct.AllInstances.ModifyPriceSingle = (p, actual) =>
{
    if (actual != 20.0f)
    {
        MolesContext.ExecuteWithoutMoles(() => p.ModifyPrice(actual));
    }
    else
    {
        Debug.WriteLine("Skipped setting price.");
    }
};

// Call the constructor that sets the price to 10.
Product p1 = new Product();
// Skip setting the price.
p1.ModifyPrice(20f);
// Set the price.
p1.ModifyPrice(21f);
.

Altri suggerimenti

MProduct.Behavior = MoleBehaviors.Fallthrough;
.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top