Optional new() constraint #2483
-
Hi All,
Today, I implement it as follows:
But the problem is that as create more enhanced bases, have to have two versions of each. One for T:new() and one for !T:new(). What is a good way for me to have a single generic class in the spirit of my ideal at the top? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
The CLR doesn't recognize generic type constraints as a part of the method signature so you are not able to overload in such a manner. I'd suggest using two different method names. |
Beta Was this translation helpful? Give feedback.
-
Basically, this is overloading by generic constraint. Not supported currently by the CLR currently, but could be very useful feature |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
new TProvider()
is syntax sugar forActivator.CreateInstance<TProvider>()
. You can do the same with reflection to test whether a public parameterless constructor exists.