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
>