I was reading this question where one answered suggests this method:
public static void Method<T>(Func<int,T> del) {
var t = del(42);
}
Which should be used like so: Method<SomeType>(x => new SomeType(x));
The Method
body is not important to this question.
If the delegate/lambda is not in that "style", the method will not work as expected. For example, if it is called like Method(x => new Foo());
it will not "work" because the purpose of the workaround will be lost.
The purpose is ensuring that T
has a constructor taking 1 parameter of type int
. I wish I could do T(int)
So is there anyway to ensure that the delegate should always be be x => new SomeType(x)
? Or is there any alternative? I have looked up a lot of places and can't come up with anything.