Forums - C# Corner

Forum guidelines
Hari Prasad

Hari Prasad

  • 1.5k
  • 4
  • 114

How to provide different arguments to an object factory?

Feb 13 2018 7:17 AM
Hi,
 

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
Hari 
 

Answers (1)