Ramblings from the Creator of WilsonDotNet.com
Thanks for the compliments though I must confess I had to ask around for the answer as well. :) I've blogged about how it is done. ~ Paul
COOL, thanks
So how do you do it then? I had exactly the same problem Mr Wilson described I couldn't figure out how to pass a generic type where the type itself was a variable? Paul can you post some indicative code?
Paul Welter details on his blog, but here's the simple code snippet: private BindingFlags internalFlags = BindingFlags.CreateInstance | (BindingFlags.NonPublic | BindingFlags.Instance); Type genericType = typeof(CustomType<>).MakeGenericType(type); object[] args = new object[] { ... }; relations = Activator.CreateInstance(genericType, internalFlags, null, args, null, null);
Aargh... I wish I had run into this post a while earlier because I struggled with this one for quite a while as well before figuring it out. Generics for all the power they present are going to take some time to digest and figure out all the oddities that they represent to the type system. Actually there's a slightly easier way: this.m_GenericEntity = Activator.CreateInstance<EntityType>();
Rick: That work's great if you know the whole type (RootType<InnerType>) as a variable, but it doesn't work if both parts are variables.
Sheesh. You guys are all way over my head. Now I have a reason to start playing with .Net 2.0 - just so I can play and learn about all this stuff and keep up with the conversation.