Creating Generic object at runtime

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kumar.senthil@gmail.com

    #1

    Creating Generic object at runtime

    Hi,
    I would like to know whether we can create a generic object at
    runtime using the type obtained using reflection.

    Type myType = Type.GetType("N amespace.Class" );
    MyClass<myType> test1 = new MyClass<myType> ();

    Basically, i need to create the generic object at run time and the
    type is decided at runtime.

    Thanks,
    Senthil

  • Joanna Carter [TeamB]

    #2
    Re: Creating Generic object at runtime

    <kumar.senthil@ gmail.com> a écrit dans le message de news:
    1134371711.3117 01.302510@g44g2 00...legr oups.com...

    | I would like to know whether we can create a generic object at
    | runtime using the type obtained using reflection.
    |
    | Type myType = Type.GetType("N amespace.Class" );
    | MyClass<myType> test1 = new MyClass<myType> ();
    |
    | Basically, i need to create the generic object at run time and the
    | type is decided at runtime.

    Certainly, you need to use Type.MakeGeneri cType in C# 2.0, this replaces a
    ..NET 1.0 method that is deprecated and that I can't remember the name of.

    {
    Type myType = Type.GetType("N amespace.Class" );

    Type[] typeArgs = new Type[] { myType };

    Type typeToCreate = typeof(MyClass< >).MakeGenericT ype(typeArgs);

    MyClass test1 = (MyClass) Activator.Creat eInstance(typeT oCreate,
    typeArgs);
    }

    Joanna

    --
    Joanna Carter [TeamB]
    Consultant Software Engineer


    Comment

    • Nicholas Paldino [.NET/C# MVP]

      #3
      Re: Creating Generic object at runtime

      Joanna,

      MakeGenericType doesn't replace anything in .NET 1.0 because it never
      existed in .NET 1.0.


      --
      - Nicholas Paldino [.NET/C# MVP]
      - mvp@spam.guard. caspershouse.co m

      "Joanna Carter [TeamB]" <joanna@not.for .spam> wrote in message
      news:OWoVkjv$FH A.356@TK2MSFTNG P12.phx.gbl...[color=blue]
      > <kumar.senthil@ gmail.com> a écrit dans le message de news:
      > 1134371711.3117 01.302510@g44g2 00...legr oups.com...
      >
      > | I would like to know whether we can create a generic object at
      > | runtime using the type obtained using reflection.
      > |
      > | Type myType = Type.GetType("N amespace.Class" );
      > | MyClass<myType> test1 = new MyClass<myType> ();
      > |
      > | Basically, i need to create the generic object at run time and the
      > | type is decided at runtime.
      >
      > Certainly, you need to use Type.MakeGeneri cType in C# 2.0, this replaces a
      > .NET 1.0 method that is deprecated and that I can't remember the name of.
      >
      > {
      > Type myType = Type.GetType("N amespace.Class" );
      >
      > Type[] typeArgs = new Type[] { myType };
      >
      > Type typeToCreate = typeof(MyClass< >).MakeGenericT ype(typeArgs);
      >
      > MyClass test1 = (MyClass) Activator.Creat eInstance(typeT oCreate,
      > typeArgs);
      > }
      >
      > Joanna
      >
      > --
      > Joanna Carter [TeamB]
      > Consultant Software Engineer
      >
      >[/color]


      Comment

      • Joanna Carter [TeamB]

        #4
        Re: Creating Generic object at runtime

        "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> a écrit
        dans le message de news: OV54uWz$FHA.160 0@TK2MSFTNGP11. phx.gbl...

        | MakeGenericType doesn't replace anything in .NET 1.0 because it never
        | existed in .NET 1.0.

        Aaahh ! Brain fart :-))

        I think I meant beta 1 of the .NET 2 framework.

        <puts tail between legs>

        Joanna

        --
        Joanna Carter [TeamB]
        Consultant Software Engineer


        Comment

        Working...