0

How can I instantiate an object using the string variable className and also pass in the constructor parameters as named pairs? (I don't want the parameters to be order dependent, and I don't want to pass them in as objects and have to cast them to their original class)

string className = "ExternalApi";
Integration integration = getIntegrationObject();
Logger logger = new Logger();

// how to do new ExternalApi(integration:integration, logger:logger)
Nathan Hanna
  • 4,643
  • 3
  • 28
  • 32

2 Answers2

0

Take a look at the parameter

params Object[] args 

and

Activator.CreateInstance
Xela
  • 2,322
  • 1
  • 17
  • 32
  • So I already know how to do it _without_ named arguments: `Activator.CreateInstance(Type.GetType(className), integration, logger);` I want to know how to do it _with_ named arguments. I don't understand how your question is suggesting I accomplish this goal. – Nathan Hanna Nov 18 '15 at 23:55
0

You can accomplish this using Activator.CreateInstance(Type type)

Please see here: https://msdn.microsoft.com/en-us/library/wccyzw83(v=vs.110).aspx

wentimo
  • 471
  • 3
  • 12