HighTechTalks DotNet Forums  

Getting the Type of a Generic Parameter

Dotnet Framework (CLR) microsoft.public.dotnet.framework.clr


Discuss Getting the Type of a Generic Parameter in the Dotnet Framework (CLR) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Rick Strahl
 
Posts: n/a

Default Getting the Type of a Generic Parameter - 12-07-2007 , 05:12 AM







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>(strin g 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 ---


Reply With Quote
  #2  
Old   
Günter Prossliner
 
Posts: n/a

Default Re: Getting the Type of a Generic Parameter - 12-07-2007 , 06:45 AM






Hello Rick!

Quote:
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




Reply With Quote
  #3  
Old   
Rick Strahl
 
Posts: n/a

Default Re: Getting the Type of a Generic Parameter - 12-07-2007 , 02:39 PM



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

Quote:
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



Reply With Quote
  #4  
Old   
Sheng Jiang[MVP]
 
Posts: n/a

Default Re: Getting the Type of a Generic Parameter - 12-07-2007 , 08:37 PM



Type.GetElementType?

--
Sheng Jiang
Microsoft MVP in VC++
"Rick Strahl" <rickstrahl (AT) hotmail (DOT) com> wrote

Quote:
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





Reply With Quote
  #5  
Old   
Rick Strahl
 
Posts: n/a

Default Re: Getting the Type of a Generic Parameter - 12-08-2007 , 01:23 AM



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 ---

--

---
Rick Strahl
West Wind Technologies
www.west-wind.com/weblog


"Mattias Sjögren" <mattias.dont.want.spam (AT) mvps (DOT) org> wrote

Quote:
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.


Reply With Quote
  #6  
Old   
Günter Prossliner
 
Posts: n/a

Default Re: Getting the Type of a Generic Parameter - 12-11-2007 , 09:12 AM



Hello Rick!

Quote:
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.
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




Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.