Hari Prasad

Hari Prasad

  • 2.1k
  • 6
  • 868

How to provide different arguments to an object factory?

Feb 13 2018 7:17 AM

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<T>() { return (IMyObj)Activator.CreateInstance(typeof(T)); } }

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

Thank You

Answers (1)