Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / December 2007

Tip: Looking for answers? Try searching our database.

Getting the Type of a Generic Parameter

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Rick Strahl - 07 Dec 2007 11:12 GMT
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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.