posts - 50, comments - 168, 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>)

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

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
Gravatar

# re: Creating generic type object dynamically using reflection

So how can I cast the created object if I just have the type? like

private List<object> ParseModel(Type model)
{
var objectList = CreateGeneric(typeof(List<>), model);
}

Thanks in advance
11/25/2009 4:02 PM | Nicolas Porto
Gravatar

# re: Creating generic type object dynamically using reflection

Unfortunatelly generics are not castable. Try doing something like this:

List<object> a = (List<object>)new List<string>();

this code will not compile. Even if you have a variable of type object (let's call it a), that holds a reference to a List<string>; you will receive InvalidCastException when you try sth like that:

List<object> a = (List<object>)a;
11/27/2009 8:10 AM | Marcin Celej
Gravatar

# re: Creating generic type object dynamically using reflection

hi,
I want to create instance of a typed class whos type is keept in the web.config and when try to this I get an exception that say Could not load x from y assembly,
any solution for this
2/24/2010 7:48 AM | reza zareian fard
Gravatar

# re: Creating generic type object dynamically using reflection

Hi,
I want to create an object based on type and i need to copy the data from another object.Any body help plz.......
5/17/2010 4:20 PM | Swetha
Gravatar

# re: Creating generic type object dynamically using reflection

Wery nice snippet, pidory)
1/12/2011 11:57 PM | Dolbotron
Gravatar

# re: Creating generic type object dynamically using reflection

Many thanks, saved me from working this out from scratch!
4/19/2011 10:08 PM | Will
Gravatar

# re: Creating generic type object dynamically using reflection

Hey,
This is great.
My only problem is that I also need a reference to this Generic collection that I'm creating, and since I can't define the generic type I can't do it.

Using your solution I managed to call invoke a call for A<t>, but this method returns object, and I need a reference to B<t>. and since I can't create T as a generic type then I can't create a reference of it - please help :)
5/12/2011 2:04 AM | Guy
Gravatar

# re: Creating generic type object dynamically using reflection

what if i need create generic type when parameter is also generic type, so need some recoursive making generic types?how to do this?
Thanks
1/25/2012 7:57 PM | dmitry
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: