Another alternative could be to create enum :
public enum ClassType { ClassA = 1, ClassB = 2, ClassC = 3 }
And then changing your method to accept this enum and return the type:
public static Type GetById(ClassType id) { //Will return null if the Type is not found, //Add true as a second parameter to throw if not found return Type.GetType(id.ToString()); }
This will remove the magic numbers from your code, but will only work as long as the class names match the enum parameters. This will make your code much smaller, but as pointed out by others, you should really question your application design because it is not entirely correct.
source share