Pretty cool trick I found today on StackOverflow.
I’ve often struggled with the fact that when you’re writing a generic method, while you can constrain the type of the method, you can only specify the constraint of having a constructor, and even then, only a default constructor (eg: new()); not one with parameters. Since I can constrain the type, which can in-turn constrain the constructor, it seems that I should be able to do
but alas, we can’t. At best we could new up the type-constrained object, but only if you’re constraining to a non-abstract class and not an Interface or abstract class, eg:
but that won’t give us the type that we passed in as our generic.
So to re-iterate what’s posted in the StackOverflow link, the solution is to pass a Func<[param type(s)], T> creator in to the method like so:
Then you just use it like this:
Which will spin up an instance of MyDerivedClass but pass the ‘1’ parameter in to it as the constructor specifies.