mardi 13 février 2018

How to provide different arguments to an object factory?

I have a library with some classes that realize the same interface:

internal class MyObj1 : IMyObj {
     public MyObj1(string param1, int param2) {}
}

internal class MyObj2 : IMyObj {
     public MyObj2(bool param1, string param2, int param3) {}
}

internal class MyObj3 : IMyObj {
     public MyObj3(string param1, int param2) {}
}

I want to create an objects factory that allows to get access to MyObj1, MyObj2, MyObj3 only by IMyObj:

public class MyObjFactory {
    public IMyObj Create<Type>() {
        return (IMyObj)Activator.CreateInstance(typeof(T));
    }
}

I don't know how to pass constructor arguments to the factory method. Any idea?

Aucun commentaire:

Enregistrer un commentaire