<ku***********@gmail.com> a écrit dans le message de news:
11**********************@g44g2000cwa.googlegroups. com...
| I would like to know whether we can create a generic object at
| runtime using the type obtained using reflection.
|
| Type myType = Type.GetType("Namespace.Class");
| MyClass<myType> test1 = new MyClass<myType>();
|
| Basically, i need to create the generic object at run time and the
| type is decided at runtime.
Certainly, you need to use Type.MakeGenericType in C# 2.0, this replaces a
..NET 1.0 method that is deprecated and that I can't remember the name of.
{
Type myType = Type.GetType("Namespace.Class");
Type[] typeArgs = new Type[] { myType };
Type typeToCreate = typeof(MyClass<>).MakeGenericType(typeArgs);
MyClass test1 = (MyClass) Activator.CreateInstance(typeToCreate,
typeArgs);
}
Joanna
--
Joanna Carter [TeamB]
Consultant Software Engineer