I’m working on WinForms application.
I have group of tests classes, each class performs different thing but 80% of classes use almost identical constructors. I’m using reflection to dynamically create class instances in run time from List, there could be dozens of different and same type of tests.
currentTest = (ISystemTest)Activator.CreateInstance(classtype,
gui,
param1,
param2,
param3,
param4,
currentProgressUpdater);
and then run the instance via Action.
As i’ve said some constructors have slightly different signature. I used to work with switch statements but via reflection it becomes easier to maintain.
To work out the issue with different constructor signatures i have either some creative solution or to create large constructor with default values for test which doesn’t need certain data.
So if you have creative solution to this problem i would love to hear it.
Example of constructors:
-
ClassName(gui, param1, param2, param3, progressUpdater)=> about 80% of current tests -
ClassName(gui, param1,param4, param5)=> about 10% of current tests -
ClassName(param4, param6)=> 10% of current tests
types are custom classes not string or ints
The problem you describe is typically solved through dependency injection. You could use a dependency injection framework such as the Managed Extensibility Framework.
edit: You can find some exemples on how to use MEF for DI here and here. Other DI frameworks (unity, autofac, Ninject) work similarily. I have myself worked with both MEF and autofac, and even though the concepts involved need a little learning to get going, they quickly become very intuitive to use and really useful. Autofac is presented as an addictive IoC container, and it’s true DI becomes addictive, whatever framework you’re using.