Generic Type Instantiation in C#

by Donovan Olivier September 1, 2011 4:08PM

Most people know that you can get a Type object from a qualified string using the Type.GetType() method.  Let's say you have a class called MyClass in your MyAssembly assembly:

namespace MyAssembly
{
    public class MyClass
    {
        public MyClass() { }
        public string Name { get; set; }
    }
}

You can use the following call to get a System.Type instance for MyClass:

var type = System.Type.GetType("MyAssembly.MyClass,MyAssembly");

But what do you do if your class is defined with a generic type?  Let's say your class is defined like this:

namespace MyAssembly
{
    public class MyClass<T>
    {
        public MyClass() { }
        public string Name { get; set; }
        public T Data { get; set; }
    }
}

If you wanted an Type object for MyClass<string>, your natural instinct would be to try this:

var type = System.Type.GetType("MyAssembly.MyClass<string>,MyAssembly");

However, this won't work.  You need to specify the type using the following notation:

var type = System.Type.GetType("MyAssembly.MyClass`1[System.String],MyAssembly");

If your class has two generic types, like this:

namespace MyAssembly
{
    public class MyClass<T1, T2>
    {
        public MyClass() { }
        public string Name { get; set; }
        public T1 Data1 { get; set; }
        public T2 Data2 { get; set; }
    }
}

If you want MyClass<string, int>, the correct notation will now be:

var type = System.Type.GetType(
                      "MyAssembly.MyClass`2[[System.String],[System.Int32]],MyAssembly");

Tags: ,

Blog

Comments

February 20, 2012 6:36PM #

craigslist used cars for sale by owner

Do you think that this automobile business will suffer due to the fact that many everyone is purchasing their automobiles web dropping the middle person?

Something is the fact that making use of sites including Craigslist takes away the guts person as well as reduces the stress to boost the price for the car to create commissions for the salesperson.  I think that the rate we're proceeding, most automobile stores will be bankrupt in Ten years while people be a little more and much more utilized to purchasing on the internet.

craigslist used cars for sale by owner United States | Reply

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



The Perpetrator

perpetrator

Donovan Olivier

software developer, architect, husband, father, son, brother, uncle, nephew, sinistral, enquirer, skeptic, cynic, anarchist, contrarian, infidel, dissident, rebel, troublemaker, dissenter, renegade.

Noise

RT @ordnung: How Target Figured Out A Teen Girl Was Pregnant Before Her Father Did - Forbes http://t.co/lhswKeO2
planetdonovan 2 days ago

“@planetdonovan: @adrianshannon random site of the day http://t.co/r9K9Zcu1” // ??! //pure genius but can my wife do that?
DerickBSeagers 5 days ago

“@planetdonovan: random site of the day http://t.co/gHfphnuS” // ??!
adrianshannon 5 days ago

2012.4 http://t.co/Fg9UqQIV
planetdonovan 5 days ago

random site of the day http://t.co/OCsZn1EJ
planetdonovan 5 days ago