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().
While there's Activator.CreateInstance<T>() that works with a generic
parameter, it doesn't accept parameters for constructors, so it looks like
CreateInstance() is what I need to use.
However, I can't figure out how to get the explicit type from the <T>
parameter. For example, here's my method signature:
public static TDataContext
GetWebRequestScopedDataContext<TDataContext>(string key, string
connectionString)
where TDataContext : DataContext, new()
{
// *** Doesn't work
Type t = typeof(TDataContext);
// *** doesn't work
t = TDataContext;
// *** This is what I need to do...
TDataContext context = Activator.CreateInstance(t,connectionString);
...
}
Is there any way to do this?
Looking at what CreateInstance<T> does with Reflector doesn't help as it
goes off into some external code that's not traceable.
Any ideas?
TIA,
+++ Rick ---
Günter Prossliner - 07 Dec 2007 12:45 GMT
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
Rick Strahl - 07 Dec 2007 20:39 GMT
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 ---

Signature
---
Rick Strahl
West Wind Technologies
www.west-wind.com/weblog
> Hello Rick!
>
[quoted text clipped - 18 lines]
>
> GP
Sheng Jiang[MVP] - 08 Dec 2007 01:46 GMT
Type.GetElementType?

Signature
Sheng Jiang
Microsoft MVP in VC++
> Hi Guenter,
>
[quoted text clipped - 35 lines]
> >
> > GP
Günter Prossliner - 11 Dec 2007 15:12 GMT
Hello Rick!
> 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
[quoted text clipped - 6 lines]
>
> with the added requirement of passing constructor parameters.
You just have to use a typeof(T) in order to call the CTOR or to use
Activator:
class C<T> where T:Something{
void foo() {
Type t = typeof(T);
ConstructorInfo ctor = t.GetConstructor(
new Type[]{typeof(int), typeof(string)}
);
if(ctor == null)
throw new ArgumentException("T is invalid, because no CTOR(int,
string) is available!");
Something instance = (Something)ctor.Invoke(new object[]{1,
"hello"});
// do whatever you want
}
}
OK?
GP
Mattias Sjögren - 07 Dec 2007 22:51 GMT
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

Signature
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Rick Strahl - 08 Dec 2007 07:23 GMT
Thanks Mattias,
I gave this another shot now and sure enough I did get it to work. Somewhere
I think the problem ended up being a missing cast - I apparently
misinterpreted the error as coming from the generic type conversion rather
than from method call signature being incorrect.
Thanks for a sanity check... <g>
+++ Rick ---

Signature
---
Rick Strahl
West Wind Technologies
www.west-wind.com/weblog
> Rick,
>
[quoted text clipped - 34 lines]
>
> Mattias