c # – instantiate a subclass based on read input

To read files from another project, I copy the write code and customize it to my needs.

I've come to a curious construct that looks really ugly, but I do not know if there's another way to handle it.

The code looks something like this:

int type = reader.readInt32 ():
BaseClass p = BaseClass.Instantiate ((BaseClassEnum) type);
object.Read (reader);

This code seems and is nice, but the BaseClass.Instantiate (BaseClassEnum) method leaves something to be desired.

Basically, it's a huge switch case statement that instantiates a subclass of the base class according to the passed type parameter.

Is there a way to avoid the change here? Can I create a dictionary that associates BaseClassEnum with a class reference that allows me to call its constructor? Something like:

dictionary bindings = new dictionary() {
{BaseClassEnum1, SubClass1},
{BaseClassEnum2, SubClass2}

// ...

// Suppose SubClass1 has a constructor SubClass1 ()
BaseClass p = new bindings[BaseClassEnum1]();

// I could even create and do a new constructor SubClass (BinaryReader Reader)
BaseCoass p = new bindings[BaseClassEnum1](Reader);

In the end, the code would look something like this:

BaseClass p = new bindings[(BaseClassEnum)reader.ReadInt32()](Reader);