471,347 Members | 1,683 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,347 software developers and data experts.

Creating Generic object at runtime

Hi,
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.

Thanks,
Senthil

Dec 12 '05 #1
3 2185
<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
Dec 12 '05 #2
Joanna,

MakeGenericType doesn't replace anything in .NET 1.0 because it never
existed in .NET 1.0.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Joanna Carter [TeamB]" <jo****@not.for.spam> wrote in message
news:OW*************@TK2MSFTNGP12.phx.gbl...
<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

Dec 12 '05 #3
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> a écrit
dans le message de news: OV**************@TK2MSFTNGP11.phx.gbl...

| MakeGenericType doesn't replace anything in .NET 1.0 because it never
| existed in .NET 1.0.

Aaahh ! Brain fart :-))

I think I meant beta 1 of the .NET 2 framework.

<puts tail between legs>

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Dec 12 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by Carlos Lozano | last post: by
12 posts views Thread by Mats Lycken | last post: by
7 posts views Thread by Nathan Sokalski | last post: by
16 posts views Thread by tshad | last post: by
1 post views Thread by Charles Law | last post: by
reply views Thread by Ronak mishra | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.