I have string inputs, and depending on the value of that input, I’d like to call a specific object’s constructor (new Object1(int), new Object2(string), new Object3(int, int), etc. all implementing an IObject interface).
Rather than doing this the obvious (and slow) way of chaining if/else statements, I’d like to do something like having a Dictionary that goes from a string to a constructor (e.g. {“object1” -> new Object1(int)} )
Is there some way I can do something like this in C#, perhaps through combining “typeof” with a constructor? I’d like to avoid a Factory, as that’d require parallel code structures, which I’d like to avoid.
Thank you in advance
You could do something like this: