Is there any way to determine a object's type based on a string, then create a new instance of that object?
I am currently doing something like this:
switch (type_str){
case "Square":
Square S = new Square();
S.DoSomethingSquarey();
DoSomething(S);
break;
case "Circle":
Circle C = new Circle();
C.DoSomethingCircley();
DoSomething(C);
break;
case "Triangle":
Triangle T = new Triangle();
T.DoSomethingTriangley();
DoSomething(T);
break;
}
All types will inherit from base class "Shape":
public static void DoSomething(Shape S){
//Doing Stuff...
}
This will quickly get out of hand to maintain as I will need to continually add shapes to the case statement. If possible, I'd like to do something like this:
Type ShapeType = Type.GetType("Square");
ShapeType X = new ShapeType();
DoSomething(X);
This will cause issues at compile time. Is there another way to simplify this case statement?
Thanks in advance.