Generic question

  • Thread starter Thread starter Enosh Chang
  • Start date Start date
E

Enosh Chang

Hi all:

I have one question about generic class.

public class Base<T>
where T : new()
{
public Base() {}

public void A()
{
T newObject = new T(parameter);
}
}

Document said I couldn't use T(parameter) but T(). Or do I have another way to do that? Thanks.
 
Enosh Chang said:
Hi all:

I have one question about generic class.

public class Base<T>
where T : new()
{
public Base() {}

public void A()
{
T newObject = new T(parameter);
}
}

Document said I couldn't use T(parameter) but T(). Or do I have
another way to do that? Thanks.

You'd have to use reflection, which bypasses some of the benefits of
generics. You can't specify that a generic type has to have a
particular constructor signature other than a parameterless
constructor.
 
Back
Top