Error using new T inside a generic

  • Thread starter Thread starter WT
  • Start date Start date
W

WT

Hello,

I am building a generic class that needs to instanciate an object of its
template class, something like

public class MyGeneric<T>
{
List<T> myList = new List<T>();
void addToLst(List l)
{
foreach(MyObj ob in l)
{
myList.Add(new T(ob));
}
}
}

new T(ob) being refused, I tried Activator.CreateInstance(T, new
Object[]{l}); but T is not a type.

How to call a constructor for the type T used in the generic ?

Thanks for help.
CS
 
new T(ob) being refused, I tried Activator.CreateInstance(T, new
Object[]{l}); but T is not a type.

How to call a constructor for the type T used in the generic ?

You can do new T(); if you've specified a parameterless constructor
constraint, but you can't specify particular constructor signatures -
you'd have to use Activator.CreateInstance (or similar). To use that,
just use typeof(T) instead of T in your code above.

Jon
 
WT,

If you want to be able to use new to create an instance of a generic
type parameter, then you have to add the constriaint, like so:

public class MyGeneric<T> where T : new()

However, that won't work in this case as you need to be able to pass
parameters, and that only works for the parameterless constructor.

If you have to call a constructor with parameters (assuming there is no
other way to configure the instance without passing parameters through the
constructor) you can use typeof(T) to get the type of T, and then pass that
to the CreateInstance method.
 
Thanks
Jon Skeet said:
new T(ob) being refused, I tried Activator.CreateInstance(T, new
Object[]{l}); but T is not a type.

How to call a constructor for the type T used in the generic ?

You can do new T(); if you've specified a parameterless constructor
constraint, but you can't specify particular constructor signatures -
you'd have to use Activator.CreateInstance (or similar). To use that,
just use typeof(T) instead of T in your code above.

Jon
 
Thanks
Nicholas Paldino said:
WT,

If you want to be able to use new to create an instance of a generic
type parameter, then you have to add the constriaint, like so:

public class MyGeneric<T> where T : new()

However, that won't work in this case as you need to be able to pass
parameters, and that only works for the parameterless constructor.

If you have to call a constructor with parameters (assuming there is no
other way to configure the instance without passing parameters through the
constructor) you can use typeof(T) to get the type of T, and then pass
that to the CreateInstance method.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

WT said:
Hello,

I am building a generic class that needs to instanciate an object of its
template class, something like

public class MyGeneric<T>
{
List<T> myList = new List<T>();
void addToLst(List l)
{
foreach(MyObj ob in l)
{
myList.Add(new T(ob));
}
}
}

new T(ob) being refused, I tried Activator.CreateInstance(T, new
Object[]{l}); but T is not a type.

How to call a constructor for the type T used in the generic ?

Thanks for help.
CS
 
Do you know if Activator method brings a huge penality in execution delay ?

CS
Jon Skeet said:
new T(ob) being refused, I tried Activator.CreateInstance(T, new
Object[]{l}); but T is not a type.

How to call a constructor for the type T used in the generic ?

You can do new T(); if you've specified a parameterless constructor
constraint, but you can't specify particular constructor signatures -
you'd have to use Activator.CreateInstance (or similar). To use that,
just use typeof(T) instead of T in your code above.

Jon
 
Do you know if Activator method brings a huge penality in execution delay ?

You'd have to test it to see whether it's significant for your usage.
For what it's worth, however, if you *do* specify the parameter
constructor constraint, the compiler just uses
Activator.CreateInstance behind the scenes anyway.

Jon
 
WT said:
Do you know if Activator method brings a huge penality in execution delay
?

Probably substantial. If you anticipate calling more than once with the
same type and the argument types are fixed, then get the ConstructorInfo,
create a delegate, cache the delegate and call using that each time.
Delegate calls are the same performance as a v-table call, which is to say
quite fast.
CS
Jon Skeet said:
new T(ob) being refused, I tried Activator.CreateInstance(T, new
Object[]{l}); but T is not a type.

How to call a constructor for the type T used in the generic ?

You can do new T(); if you've specified a parameterless constructor
constraint, but you can't specify particular constructor signatures -
you'd have to use Activator.CreateInstance (or similar). To use that,
just use typeof(T) instead of T in your code above.

Jon
 
Back
Top