I have a certain amount of classes which inherit from an abstract class:
abstract public class baseClass
{
//logics
}
public class child1 : baseClass
{
}
public class child2 : baseClass
{
}
now I've got some management class which will have to create one of these classes depending on a enum which will have as value the same name so like this:
public enum ClassType
{
child1,
child2
}
public class Manager
{
private List<baseClass> _workers;
public void Initialize(ClassType type)
{
//what logics to put here? (resulting in correctChild)
_workers.Add(correctChild);
}
}
I was thinking of typeof but don't know quite how to implement it. Note: in this example it's 2 classes but in the real case it's an arbitrary amount of classes.
If you dont want to use if statements, can you use case statements? like:
There are many way to differentiate between types.
you can use the
is
operatoror you could use the
as
operatoror what you described yourself already, the
typeof
operator.What I am sensing though, is that if you have a strong need for type casting, there might be something else at hand. Maybe you can solve your problem by Polymorphism. Then you don't have to distinguish between all the different types in lengthy
if-else
orcase-switch
constructions, but you can delegate the 'different behavior' to your subclasses.If you don't need the overhead of attributes and you are running this from the same assembly - you can just use the executing assembly to load the type via reflection.
Activator.CreateInstance
can take care of building the object for you dynamically.Have a look at Activator.CreateInstance()
so the code would look something like
I'm actually familiar with such an implementation, i've used it myself. I would also recommend looking into this article that explains how you can actually use attributes on the enum values, to store the actual class names. You can even use it as an extension.
The code will then look something like