jeudi 4 juillet 2019

Choose Implementation of Interface by Variable

In my GUI there is a drop down list of customers. Each customer has a class associated with it, containing the methods associated with that customer (for instance loading their invoice format etc). Each of the customer classes implements the interface "ICustomer", but the contents of the methods differ.

The calling class has an ICustomer property - I would like to set that to be the class represented by the selected value in the dropdown. Something a bit like this pseudocode:

public interface ICustomer
{
    int GetInvoice();
}

and

Class Caller()
{
    public ICustomer Customer { get; set; }
    public void Choose(string customerName)
    {
        Customer = //??? ["Get class where name == customerName"];
        var foo = Customer.GetInvoice();
    }
}

From my very limited understanding and muddled internet searching, I think I need to use reflection to achieve this, but I have so far failed to return a specific, set-at-runtime class from the interface. How would I go about achieving this?





Aucun commentaire:

Enregistrer un commentaire