Let’s say I have base class FooParent, and it has a numerous amount of FooChildren. At runtime I must create an instance of one of the FooChildren. How would I do this? I realize I could create a huge map (and use delegates) or a huge switch/case statement, but that just seems a bit sloppy. In something like PHP, I can easily create a class dynamically like this:
$className="FooClass";
$myNewFooClass=new $className; //makes a new instance of FooClass
(you can also do this using reflection).
Does .NET have anything like this? Is reflection an option, and does it have any performance penalties? If not, what other options do I have?
The type of class will be determined by a JSON request. The variable could be anything I want..it could be an integer if I wanted to do an enum, or it could be the full class name. I haven’t created it yet so I’m undecided.
You can do it with reflection if you really want, but there will be performance penalties. Whether they’re significant or not will depend on your exact situation.
Depending on exactly what you want to do, I’d quite possibly go with either a switch/case statement or a map, as you suggest. In particular, that would be useful if you need to pass different arguments to different constructors based on the type you’re constructing – doing that via reflection would be a bit of a pain in that you’d already be special-casing different types.
EDIT: Okay, so we now know that there will always be a parameterless constructor. In that case, your JSON could easily contain the class name without the namespace (if they’re all in the same namespace) and your method could look something like this:
Where do you determine the name to use? If it’s passed in somewhere, a switch statement can be very simple when reformatted away from the norm a little:
Mind you, having just written that out I’m not sure it has any real benefit (other than performance) over using
Type.GetType(name)and thenActivator.CreateInstance(type).How does the caller know the class name to pass in? Will that definitely be dynamic? Is there any chance you could use generics? The more you could tell us about the situation, the more helpful we could be.