I have following code.
string name = GetClassName();
switch (name)
{
case "Class1":
new Generic<Class1>().DoSomething();
break;
case "Class2":
new Generic<Class2>().DoSomething();
break;
case "Class3":
new Generic<Class3>().DoSomething();
break;
default:
break;
}
There might be more than 50 values of name, and seems it's not wise to have more than 50 cases. I think there must be some generic way to do that.
like
string name = GetClassName();
new Generic<ConverToClass(name)>().DoSomething();
Let me make my question clear. Thanks for your reply.
Actually, this code is just a snippet of my project, which is written in .NET 5.0.
I have a generic class EntityDao to CRUD entities, like Create, Update, Remove, EntityType might be User, Setting and etc. And there is a GRPC service, which accpets client request including entity type as string, entity data to CRUD entties.
class Request {
string Type; // Entity type, which is the string of EntityType, like "User", "Setting".
EntityAction Action; //Create, Update, Remove and etc.
ByteString Data; // Holding entity serialized data.
}
There is also a method to handle the request from client.
Request request = GetClientRequst()
//container is IContainer of Autofac.
switch (request.Type) {
case "User":
container.Reslove<EntityDao<User>>().Create(request.Data); // Assuming Action is Create
break;
case "Setting":
container.Reslove<EntityDao<Setting>>().Create(request.Data); // Assuming Action is Create
break;
case "Other": // Have more than 50 entity types.
break;
}