I have a dictionary that contains classes. However, I have a lot of classes to add to the dictionary and I would like to eliminate the long list of adds, because it’s starting to look messy. I was thinking of having all the data in a file and loading it to add to the dictionary, but then I realized that every time I wanted to create and add a new class, I would have to modify the file. I would prefer to have to include something in my newly created class that would automatically add it to the dictionary. I’m not even sure this is possible, so I would appreciate any help.
Share
Try to use reflection to dynamically locate all the required classes. Create an instance of
Assemblywhere your classes are defined and try to filter them out. An example below shows how to locate all the classes whose name ends withTask.I’ve used
Activator.CreateInstancemethod to create instances of your classes, but this might not be possible in your case depending on whether they have parameterless contructors etc.Another option might be to use one of the available IoC containers like autofac but I need more details on your problem before I can give a proper advice on this.