How to dynamically create generic C# object using reflection?

Check out this article and this simple example Quick translation of same to your classes var d1 = typeof(Task); Type typeArgs = { typeof(Item) }; var makeme = d1. MakeGenericType(typeArgs); object o = Activator. CreateInstance(makeme) Per your edit: For that case, you can do this var d1 = Type. GetType("GenericTest.

TaskA`1"); // GenericTest was my namespace, add yours Type typeArgs = { typeof(Item) }; var makeme = d1. MakeGenericType(typeArgs); object o = Activator. CreateInstance(makeme) To see where I came up with backtick1 for the name of the generic class, see this article.

Check out this article and this simple example. Quick translation of same to your classes ... var d1 = typeof(Task); Type typeArgs = { typeof(Item) }; var makeme = d1. MakeGenericType(typeArgs); object o = Activator. CreateInstance(makeme); Per your edit: For that case, you can do this ... var d1 = Type.

GetType("GenericTest. TaskA`1"); // GenericTest was my namespace, add yours Type typeArgs = { typeof(Item) }; var makeme = d1. MakeGenericType(typeArgs); object o = Activator. CreateInstance(makeme); To see where I came up with backtick1 for the name of the generic class, see this article.

Thank you very much! – Jeff Jul 20 '09 at 2:34 I never kenw about the backtick! – Jeff Jul 22 '09 at 0:56 nice!

Thanks for the post. – bytebender Feb 26 '10 at 17:51 Is the backtick required, i.e. If it is ommited, does the compiler assume it is 1?

– Richard DesLonde Mar 3 at 7:01 Thanks for linking to my blog article "Using Reflection to Instantiate a Generic Class in C# . Net"(omegacoder. Com/?

P=38) as the "Simple Example". :-) I am glad the article is finding new life. – OmegaMan May 16 at 18:48.

I guess I was missing something. :-) – Ben M Jul 20 '09 at 2:35 1 You are not missing anything. It's me who wasn’t thinking straight.

I shouldn’t have drunk that much alcohol last nite! – Jeff Jul 20 '09 at 2:41.

Indeed you would not be able to write the last line. But you probably don't want to create the object, just for the sake or creating it. You probably want to call some method on your newly created instance.

You'll then need something like an interface : public interface ITask { void Process(object o); } public class Task : ITask { void ITask. Process(object o) { if(o is T) // Just to be sure, and maybe throw an exception Process(o as T); } public void Process(T o) { } } and call it with : Type d1 = Type. GetType("TaskA"); //or "TaskB" Type typeArgs = { typeof(Item) }; Type makeme = d1. MakeGenericType(typeArgs); ITask task = Activator.

CreateInstance(makeme) as ITask; // This can be Item, or any type derived from Item task. Process(new Item()); In any case, you won't be statically cast to a type you don't know beforehand ("makeme" in this case). ITask allows you to get to your target type.

If this is not what you want, you'll probably need to be a bit more specific in what you are trying to achieve with this.

Ho well, the question has changed :) – Jerome Laban Jul 20 '09 at 2:53 I do have something like Process() in Task. And in my case I actually don't care about the last line anymore as you stated I am simply calling task.Process() therefore whether is able to code the last line becomes irreverent. – Jeff Jul 20 '09 at 3:06.

Make sure you're doing this for a good reason, a simple function like the following would allow static typing and allows your IDE to do things like "Find References" and Refactor -> Rename. Public Task factory (String name) { Task result; if (name. CompareTo ("A") == 0) { result = new TaskA (); } else if (name.

CompareTo ("B") == 0) { result = new TaskB (); } return result; }.

Why are you using . CompareTo? Why not == or .

Equals (if you want more control). Maybe a switch would even be better. – Zyphrax Jul 21 '09 at 22:59 I never checked if C# does a string comparison on == instead of a reference comparison.

CompareTo and Equals should have the same run time efficiency if they were implemented correctly. A switch block wouldn't have the same speed-ups as putting an integer in a switch block; it would compile to an if-else block. – clemahieu Jul 21 '09 at 23:29.

But you probably don't want to create the object, just for the sake or creating it. You probably want to call some method on your newly created instance. In any case, you won't be statically cast to a type you don't know beforehand ("makeme" in this case).

ITask allows you to get to your target type. If this is not what you want, you'll probably need to be a bit more specific in what you are trying to achieve with this.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions