generically instantiating types....

  • Thread starter Thread starter Peter K
  • Start date Start date
P

Peter K

Hi

is there anyway to specify that a type must have a specific constructor?

For example, can I specify that a type T has a constructor which accepts an
XmlNode, so I know I can instantiate like:

T t = new T(node);

Thanks,
Peter
 
Hi

is there anyway to specify that a type must have a specific constructor?

For example, can I specify that a type T has a constructor which accepts
an
XmlNode, so I know I can instantiate like:

T t = new T(node);

No. The only constructor constraint possible is the no-argument
constructor.

I would think that if you have enough specific knowledge about the
argument for the constructor for a type parameter for a generic type or
method, then it's very likely that the generic type or method doesn't
actually have that much value being generic, or you should be able to
design the classes being used so that the type parameter can be
constrained to some base class in which you can put a factory method for
the purpose of constructing new objects of the appropriate type.

Barring that, you can always use reflection to instantiate objects using
constructors that take arguments.

Pete
 
Back
Top