Home All Groups Group Topic Archive Search About
Author
3 May 2007 2:53 PM
PJ6
I just stumbled across an apparent generics limitation that I don't
understand. In a generic class's type list, why can't the compiler be made
aware of the types' contructors? Right now the compiler can only pay
attention to the New declaration to get its contructor logic, and as such,
only call paramaterless constructors. It strikes me as unnecessarily
restrictive, and I wonder what the reason behind that was.

Paul

Author
3 May 2007 3:44 PM
Tom Shelton
On May 3, 8:53 am, "PJ6" <n***@nowhere.net> wrote:
> I just stumbled across an apparent generics limitation that I don't
> understand. In a generic class's type list, why can't the compiler be made
> aware of the types' contructors? Right now the compiler can only pay
> attention to the New declaration to get its contructor logic, and as such,
> only call paramaterless constructors. It strikes me as unnecessarily
> restrictive, and I wonder what the reason behind that was.
>
> Paul

While I don't know the reason, I agree with you that it seems
restrictive.  Not only can it only call parameterless constructors, it
can only call them if they are public.  To be honest though, the
couple of times that this has been an issue for me, I have been able
to use Activator.CreateInstance to work around the problem (since it
can call non-public constructors :)

--
Tom Shelton
Author
3 May 2007 3:53 PM
PJ6
"Tom Shelton" <tom_shel***@comcast.net> wrote in message
news:1178207044.372217.100600@o5g2000hsb.googlegroups.com...
> While I don't know the reason, I agree with you that it seems
> restrictive.  Not only can it only call parameterless constructors, it
> can only call them if they are public.  To be honest though, the
> couple of times that this has been an issue for me, I have been able
> to use Activator.CreateInstance to work around the problem (since it
> can call non-public constructors :)

Mmm, forgot about Activator.CreateInstance.

Thanks, that will do the trick as far as keeping my design the way I want it
:)

Paul