generics and reflection

G

Guest

Is it possible to instantiate a generic class when the parametrized type
isn't known until runtime? For example, in Indigo you can deploy a web
service with something like...
ServiceHost<Foo> sh = new ServiceHost<Foo>(myURI);
sh.Open();
What if the type "Foo" isn't known until runtime? Can I use reflection to
create a Type object corresponding to the type ServiceHost<Foo>, then use
Activator.CreateInstance()? How would I do this? Do I use
System.Reflection.Emit.GenericTypeParameterBuilder?
Thanks,
-- Jim Robertson
 
G

Guest

I tried something like...

string typeName = "System.ServiceModel.ServiceHost`1[myNameSpace.Foo]";
Type myType = Type.GetType(typeName);
Object fooService = Activator.CreateInstance(myType, args);

But this didn't work. The call Type.GetType(typeName) returned null.
 
J

Joanna Carter \(TeamB\)

What if the type "Foo" isn't known until runtime? Can I use reflection to
create a Type object corresponding to the type ServiceHost<Foo>, then use
Activator.CreateInstance()? How would I do this? Do I use
System.Reflection.Emit.GenericTypeParameterBuilder?

Try the following :

Type[] paramTypes = new Type[] { typeof(Foo) };

Type BoundType =
typeof(ServiceHostType<>).BindGenericParameters(paramTypes);

Activator.CreateInstance(BoundType));

Joanna
 
R

Richard Blewett [DevelopMentor]

ServiceHost isn't in your assembly or mscorlib so you need to specify which assembly to find the type:

string typeName = "System.ServiceModel.ServiceHost`1[myNameSpace.Foo], System.ServiceModel, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

I tried something like...

string typeName = "System.ServiceModel.ServiceHost`1[myNameSpace.Foo]";
Type myType = Type.GetType(typeName);
Object fooService = Activator.CreateInstance(myType, args);

But this didn't work. The call Type.GetType(typeName) returned null.
 
J

John Pouliezos

Joanna said:
"Jim Robertson" <[email protected]> a écrit dans le
message de news: (e-mail address removed)...

What if the type "Foo" isn't known until runtime? Can I use reflection to
create a Type object corresponding to the type ServiceHost<Foo>, then use
Activator.CreateInstance()? How would I do this? Do I use
System.Reflection.Emit.GenericTypeParameterBuilder?


Try the following :

Type[] paramTypes = new Type[] { typeof(Foo) };

Type BoundType =
typeof(ServiceHostType<>).BindGenericParameters(paramTypes);

Activator.CreateInstance(BoundType));

Joanna
I had the same problem, and this workd fine. What I need to do, is
actually cast the result of the Activator.CreateInstance not to just an
object, but to a generic collection class of type "Foo". Regarding the
example, what I mean is:

ServiceHost<Foo> sh = Activator.CreateInstance(BoundType));

how can I do this, if I dont know foo, until runtime? Any help ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top