473,387 Members | 1,486 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

generics and reflection

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
Nov 17 '05 #1
7 4305
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.

"Jim Robertson" wrote:
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

Nov 17 '05 #2
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.

"Jim Robertson" wrote:
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

Nov 17 '05 #3
"Jim Robertson" <Ji**********@discussions.microsoft.com> a écrit dans le
message de news: BA**********************************@microsoft.com...
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(pa ramTypes);

Activator.CreateInstance(BoundType));

Joanna

--
Joanna Carter
Consultant Software Engineer
Nov 17 '05 #4
"Jim Robertson" <Ji**********@discussions.microsoft.com> a écrit dans le
message de news: BA**********************************@microsoft.com...
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(pa ramTypes);

Activator.CreateInstance(BoundType));

Joanna

--
Joanna Carter
Consultant Software Engineer
Nov 17 '05 #5
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.
Nov 17 '05 #6
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.
Nov 17 '05 #7
Joanna Carter (TeamB) wrote:
"Jim Robertson" <Ji**********@discussions.microsoft.com> a écrit dans le
message de news: BA**********************************@microsoft.com...

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.GenericTypeParameterBuild er?

Try the following :

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

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

Activator.CreateInstance(BoundType));

Joanna

--
Joanna Carter
Consultant Software Engineer

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 ?
Nov 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: andrew queisser | last post by:
I've been trying to dynamically create a class DevT that's derived from a generic base GenBase<T>. It doesn't seem to work. I'm attaching a code sample below that illustrates the problem. ...
7
by: Gene Vital | last post by:
Hi all, I need some help in understanding how to use Generics. I have a class based on a user control that can be put on any Container at runtime, I want to be able to call a method on the...
1
by: uttara | last post by:
I have a generic collection which I am using in classes to store a collection of embedded objects. Class Employee: IEntity { Private string mName; Private int mEmployeeID; …. Private...
9
by: sloan | last post by:
I'm not the sharpest knife in the drawer, but not a dummy either. I'm looking for a good book which goes over Generics in great detail. and to have as a reference book on my shelf. Personal...
1
by: Vladimir Shiryaev | last post by:
Hello! Exception handling in generics seems to be a bit inconsistent to me. Imagine, I have "MyOwnException" class derived from "ApplicationException". I also have two classes...
4
by: Cedric Rogers | last post by:
I wasn't sure if I could do this. I believe I am stretching the capability of what generics can do for me but here goes. I have a generic delegate defined as public delegate bool...
5
by: Wiktor Zychla [C# MVP] | last post by:
suppose I have a class class MyContainter { List<something1list1; List<something2list2; ... List<somethingnlistn; othertype1 otherfield1; ...
10
by: Marc Gravell | last post by:
Given a generic method "of T", is there a good way of ensuring that any static ctor on T has executed? Following code demonstrates (TestClass1) that via generics you can use the Type instance long...
7
by: =?Utf-8?B?TXJOb2JvZHk=?= | last post by:
Say I have a class that has a generics List as follows: public List<MyClassmyClassList = new List<MyClass>(); and I want to create another class which tries to add an element of MyClass to...
8
by: Tony Johansson | last post by:
Hello! I have read that in practice, casting proved to be several times faster than using a generic. So the main reason to use generics is not that the performance is better because that's...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.