473,396 Members | 2,139 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 2250
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Andy | last post by:
Can anyone tell me if it is possible to dynamically create generic collections? simply put I want to be able to something like this : public object test(SomeBaseClass param1) { if...
15
by: Carlos Lozano | last post by:
Hi, What is the right way to create an OCX COM component. The component is already registerred, but can't create an instance. I am using the reference to the interop module created. If I use...
12
by: Mats Lycken | last post by:
Hi, I'm creating a CMS that I would like to be plug-in based with different plugins handling different kinds of content. What I really want is to be able to load/unload plugins on the fly without...
7
by: Nathan Sokalski | last post by:
I am having a problem saving an image with the same name it originally had. I have two similar versions of my code, one in which I close the FileStream used to open the original image before saving,...
16
by: Asaf | last post by:
I am trying to create and use a COM object with C#.NET 2005. The assembly is set to "Register for COM interop" but when I am trying to call it from VB on Word 2003 I am getting this error: ...
16
by: tshad | last post by:
This is a little complicated to explain but I have some web services on a machine that work great. The problem is that I have run into a situation where I need to set up my program to access one...
0
by: Pucca | last post by:
Hi, I got the following code in a project for creating dll but I'm getting compiling error. I copied the code (from Main()) from Ineternet but becuase I don't know how COM interop and how to code...
0
by: Mikkel Blanné | last post by:
I haven't been able to find any references to using this combination of technologies (remoting + generic methods + method overloading). I don't think the problem has to do with C#, but I couldn't...
1
by: Charles Law | last post by:
I have a base class MyBaseClass, and several classes that inherit from it: MyClass1, MyClass2, etc. The base class implements IEnumerable(Of IMyBaseClassRow). The base class has a DataTable...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.