![]() | |
![]() |
| | Thread Tools | Search this Thread | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I need to figure out how to get the type of a generic parameter in order to create an instance of this generic type using Activator.CreateInstance(). |
#3
| |||
| |||
|
|
Hello Rick! I need to figure out how to get the type of a generic parameter in order to create an instance of this generic type using Activator.CreateInstance(). You have to construct the concrete Type before calling Activator.CreateInstance. e.g. class X<T> {} Type genericType = typeof(X<>); Type concreteType = genericType.MakeGenericType(typeof(int)); object instance = Activator.CreateInstance(concreteType); // will be X<int OK? GP |
#4
| |||
| |||
|
|
Hi Guenter, Thanks for the help, but unfortunately this doesn't address the issue. The problem is that I don't actually need create a type from the generic parameter, but need to instantiate the type specified BY the parameter. The generic parameter is an object and I need to instantiate that type. IOW, it's not - for example - List<T> that I need to instantiate but T itself. In essence what I need to do is: Activator.CreateInstance<T>(); with the added requirement of passing constructor parameters. +++ Rick --- -- --- Rick Strahl West Wind Technologies www.west-wind.com/weblog "Günter Prossliner" <g.prossliner/gmx/at> wrote in message news:up%23fd8MOIHA.3940 (AT) TK2MSFTNGP05 (DOT) phx.gbl... Hello Rick! I need to figure out how to get the type of a generic parameter in order to create an instance of this generic type using Activator.CreateInstance(). You have to construct the concrete Type before calling Activator.CreateInstance. e.g. class X<T> {} Type genericType = typeof(X<>); Type concreteType = genericType.MakeGenericType(typeof(int)); object instance = Activator.CreateInstance(concreteType); // will be X<int OK? GP |
#5
| |||
| |||
|
|
Rick, // *** Doesn't work Type t = typeof(TDataContext); That should work. In what way does it fail for you? // *** doesn't work t = TDataContext; This wont work but also isn't needed. // *** This is what I need to do... TDataContext context = Activator.CreateInstance(t,connectionString); As long as you cast the return value to the correct type that should work too. The following simplified example compiles and runs fine for me class Test { public Test(string s) { Console.WriteLine(s); } static void Create<T>(string s) { Activator.CreateInstance(typeof(T), s); } static void Main() { Create<Test>("Hello World"); } } Mattias -- Mattias Sjögren [C# MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com Please reply only to the newsgroup. |
#6
| |||
| |||
|
|
The problem is that I don't actually need create a type from the generic parameter, but need to instantiate the type specified BY the parameter. The generic parameter is an object and I need to instantiate that type. IOW, it's not - for example - List<T> that I need to instantiate but T itself. In essence what I need to do is: Activator.CreateInstance<T>(); with the added requirement of passing constructor parameters. |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
| |