posts - 50, comments - 126, trackbacks - 6

My Links

News



View Marcin Celej's profile on LinkedIn

Archives

Post Categories

Creating generic type object dynamically using reflection

I had a problem some time ago. I needed to create generic type object dynamically. I couldn't find how to do it. Now, when I know, I can share the mysterious knowledge.

public static object CreateGeneric(Type generic, Type innerType, params object[] args)
{
	System.Type specificType = generic.MakeGenericType(new System.Type[] { innerType });
	return Activator.CreateInstance(specificType, args);
}

To create a genric List of string, use the following code:

CreateGeneric(typeof(List<>), typeof(string));

If the generic type constructor requires parameters, they can be passed to the CreateGeneric method.

The CreateGeneric method supports only one generic argument but it can be easily modified to support more (e.g. creation of Dictionary<T, U>)

Print | posted on Saturday, March 24, 2007 10:46 PM |

Feedback

Gravatar

# re: Creating generic type object dynamically using reflection

Very useful, thanks!

Had read somewhere that creating generic types on-the-fly with having a "T" to use was a pain, but your sample it pretty simple.

Production code here we come!
7/22/2007 8:37 PM | Simon Stewart
Gravatar

# re: Creating generic type object dynamically using reflection

It is not working for System.Nullable<int>. The object created is an int, not a "new Nullable<int>(int)".

I am currently working on it.
8/12/2007 10:03 AM | Jason White
Gravatar

# re: Creating generic type object dynamically using reflection

Thank you Very much..
1/29/2008 4:40 AM | Mohamed Bakr
Gravatar

# re: Creating generic type object dynamically using reflection

So.. if you created your List<T> object from your method how could you then pull out specific indexes etc. from the object returned?

So for example..

Object o = CreateGeneric(typeof(List<>), typeof(string));
string 1 = o[0];// ??
3/3/2008 6:10 AM | Justin
Gravatar

# re: Creating generic type object dynamically using reflection

You have two choices:

1. Cast the created object
2. Use reflection to access the indexer

The choice is yours.
3/3/2008 8:00 AM | Marcin Celej
Gravatar

# re: Creating generic type object dynamically using reflection

thank you, this was a ncie code snippet to find. What do you think of the following approach - I think it's a little more concise and eliminate the need to cast from object to your specific type since you define the type in the create call...

public static T CreateGeneric<T>(params object[] args)
{
Type generic = typeof(T);
return (T)Activator.CreateInstance(generic, args);
}

list = CreateGeneric<List<string>>(null);

10/5/2008 6:38 AM | Shannon Richards
Gravatar

# re: Creating generic type object dynamically using reflection

Hi, it actually doesn't have a sense to create a generic method that creates generic type dynamically as you can just create it in your code. Let me explain this:

If you can call CreateGeneric<List<string>>() in your code than you can with the same effort write new List<string>(null)

I hope it explains it well. I used a sample "CreateGeneric(typeof(List<>), typeof(string));" just to explain how it works but in such case you should not use the dynamic object creation.
10/5/2008 6:46 AM | Marcin Celej
Gravatar

# re: Creating generic type object dynamically using reflection

hi

can you help me?
i have a Table, in one column , i have controls type and i want to create dynamically and set propertits, for example , tblFrame , it has column with data : Image, Panel, ...
and i want to reda type from database and create controls and set properties
my email is Fatemeh.akbari@gmail.com
best regards
10/11/2008 7:26 PM | Fatemeh
Gravatar

# re: Creating generic type object dynamically using reflection

Just what I was looking for, thanks! It's so easy once you see it.
10/27/2008 1:33 PM | Jim
Gravatar

# re: Creating generic type object dynamically using reflection

thanks !
6/13/2009 7:32 AM | Igor
Gravatar

# re: Creating generic type object dynamically using reflection

Tks so much... This snippet save me today =D
7/17/2009 7:33 AM | Bruno Feliciano
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 

Powered by: