This question has already been answered:
- Ways to create objects in C # 2 response
I am writing a scheduler for different tasks of working with the database, it is loading, unloading, data processing. all actions are implemented as separate classes and inherited from an abstract class and must implement the required Run () method
The tasks themselves are stored in the database table. One of the fields indicates the class name of the Run () method of which you want to run on a schedule. accordingly, at startup, you need to determine which class instance to create
private void TimerTask(object Obj) { IsRuning = true; SetNextStartTask(); switch (Metod) { case "ExportXML0": { Task task = Task.Factory.StartNew(() => { ExportData export = new ExportXML0(Id); export.Run(); }); if (task.IsCompleted) { task.Dispose(); } break; } case "ExportUniversalToTXT": { Task task = Task.Factory.StartNew(() => { ExportData export = new ExportUniversalToTXT(Id); export.Run(); }); if (task.IsCompleted) { task.Dispose(); } break; } } IsRuning = false; } Now there are relatively few classes, about 30. When there are 100+, then this method will look scary. How can you simplify this choice?
In some languages, for example, SQL or Caché Basic, you can write some text and execute it, how beautiful it is to do it in C #