Connecting Tech Pros Worldwide Forums | Help | Site Map

Error using new T inside a generic

WT
Guest
 
Posts: n/a
#1: Aug 22 '07
Hello,

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

public class MyGeneric<T>
{
List<TmyList = 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



Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#2: Aug 22 '07

re: Error using new T inside a generic


On Aug 22, 4:40 pm, "WT" <W...@newsgroups.nospamwrote:

<snip>
Quote:
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

Nicholas Paldino [.NET/C# MVP]
Guest
 
Posts: n/a
#3: Aug 22 '07

re: Error using new T inside a generic


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<Twhere 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]
- mvp@spam.guard.caspershouse.com

"WT" <WT@newsgroups.nospamwrote in message
news:uce9RLN5HHA.2752@TK2MSFTNGP06.phx.gbl...
Quote:
Hello,
>
I am building a generic class that needs to instanciate an object of its
template class, something like
>
public class MyGeneric<T>
{
List<TmyList = 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
>

WT
Guest
 
Posts: n/a
#4: Aug 22 '07

re: Error using new T inside a generic


Thanks
"Jon Skeet [C# MVP]" <skeet@pobox.coma écrit dans le message de news:
1187797476.477444.169200@x35g2000prf.googlegroups. com...
Quote:
On Aug 22, 4:40 pm, "WT" <W...@newsgroups.nospamwrote:
>
<snip>
>
Quote:
>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
Guest
 
Posts: n/a
#5: Aug 22 '07

re: Error using new T inside a generic


Thanks
"Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard.caspershouse.coma écrit
dans le message de news: ONxdyNN5HHA.1168@TK2MSFTNGP02.phx.gbl...
Quote:
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<Twhere 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]
- mvp@spam.guard.caspershouse.com
>
"WT" <WT@newsgroups.nospamwrote in message
news:uce9RLN5HHA.2752@TK2MSFTNGP06.phx.gbl...
Quote:
>Hello,
>>
>I am building a generic class that needs to instanciate an object of its
>template class, something like
>>
>public class MyGeneric<T>
>{
> List<TmyList = 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
>>
>
>

WT
Guest
 
Posts: n/a
#6: Aug 23 '07

re: Error using new T inside a generic


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

CS
"Jon Skeet [C# MVP]" <skeet@pobox.coma écrit dans le message de news:
1187797476.477444.169200@x35g2000prf.googlegroups. com...
Quote:
On Aug 22, 4:40 pm, "WT" <W...@newsgroups.nospamwrote:
>
<snip>
>
Quote:
>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
>

Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#7: Aug 23 '07

re: Error using new T inside a generic


On Aug 23, 8:31 am, "WT" <W...@newsgroups.nospamwrote:
Quote:
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

Ben Voigt [C++ MVP]
Guest
 
Posts: n/a
#8: Aug 27 '07

re: Error using new T inside a generic



"WT" <WT@newsgroups.nospamwrote in message
news:u3UXbeV5HHA.5724@TK2MSFTNGP05.phx.gbl...
Quote:
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.
Quote:
>
CS
"Jon Skeet [C# MVP]" <skeet@pobox.coma écrit dans le message de news:
1187797476.477444.169200@x35g2000prf.googlegroups. com...
Quote:
>On Aug 22, 4:40 pm, "WT" <W...@newsgroups.nospamwrote:
>>
><snip>
>>
Quote:
>>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
>>
>
>

Closed Thread