I have a set of configuration objects whose constructors all take the same parameters. Is it possible to assign a constructor to a Func, or something similar, so that the constructor can be invoked in a generic way?
For example:
public class FooConfig : ConfigBase {
public FooConfig(int a) { ... }
}
public class BarConfig : ConfigBase {
public BarConfig(int a) { ... }
}
In calling code:
Func<int, ConfigBase> fooConfigCtr = FooConfig.FooConfig; // Obviously this doesn't work
FooConfig fooConfig = (FooConfig) fooConfigCtr(12345);
The calling code example is not to show the actual use, but just to show why I am having difficulty using Func for this case.
A workaround I have found is to provide a static Factory method on each class with the same signature, and have that call a private constructor. However it is slightly non-optimal.