If I understand you correctly, MyTypes , and the actual type of the class expresses the same thing. In this case, you need to have some kind of mapping from your MyTypes to the actual classes, for example:
static Dictionary<MyTypes, Type> typeMapping = new Dictionary<MyTypes, Type> { { MyTypes.Type1, typeof(class2) }, { MyTypes.Type2, typeof(class3) }, { MyTypes.Type2, typeof(class4) }, };
Then you can use reflection to create an instance of the specified type:
private class1 myFunction(MyType t, int id, string name) { class1 obj = (class1)typeMapping[t].GetConstructor(Type.EmptyTypes).Invoke(null); obj.type = t ; obj.id = id ; obj.name = name; return obj; }
svick source share