generics question

  • Thread starter Thread starter PJ6
  • Start date Start date
P

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
 
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 said:
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
 
Back
Top