using Generics dynamically (?)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gazarsgo

    using Generics dynamically (?)

    This seems to be a bit of a contradiction, but how can I construct a
    Generic class using a System.Type variable that is assigned at run
    time? Is there some parallel to the Generics concept that extends to
    having strictly-typed classes at run-time?

    I would gladly give up the compile-time errors if I could get rid of
    all these CType()s :)

  • Herfried K. Wagner [MVP]

    #2
    Re: using Generics dynamically (?)

    "Gazarsgo" <gazarsgo@gmail .com> schrieb:[color=blue]
    > This seems to be a bit of a contradiction, but how can I construct a
    > Generic class using a System.Type variable that is assigned at run
    > time?[/color]

    Maybe you are looking for 'Type.MakeGener icType' and
    'Activator.Crea teInstance'?
    [color=blue]
    > Is there some parallel to the Generics concept that extends to
    > having strictly-typed classes at run-time?[/color]

    Strict typing is a compile-time feature.

    --
    M S Herfried K. Wagner
    M V P <URL:http://dotnet.mvps.org/>
    V B <URL:http://classicvb.org/petition/>

    Comment

    • Jay B. Harlow [MVP - Outlook]

      #3
      Re: using Generics dynamically (?)

      Gazarsgo,
      In addition to the other comments.

      | Is there some parallel to the Generics concept that extends to
      | having strictly-typed classes at run-time?
      That is the major point behind Generics. You have strictly-typed classes at
      compile & runtime

      For example:

      ' create a loosely typed collection of integers in .NET 1.x
      Dim aList As New ArrayList
      aList.Add(1)
      aList.Add(2)
      aList.Add("Sill y Me") ' allowed as its loosely typed!

      ' create a strictly-typed collection of integers in .NET 2.0
      Dim aList As New List(Of Integer)
      aList.Add(1)
      aList.Add(2)
      aList.Add("Sill y Me") ' Compile error as its strictly typed!


      | This seems to be a bit of a contradiction, but how can I construct a
      | Generic class using a System.Type variable that is assigned at run
      | time?
      You should be able to use Type.MakeGeneri cType & Activator.Creat eInstance,
      something like:

      Dim aType As Type = GetType(String)

      Dim theCollectionTy pe As Type =
      GetType(System. Collections.Obj ectModel.Collec tion(Of ))

      Dim myColType As Type = theCollectionTy pe.MakeGenericT ype(aType)

      Dim mycol As Object = Activator.Creat eInstance(myCol Type)


      HOWEVER: Without knowing the actual type at compile time, you will only be
      able to use late binding, Reflection, & object variables with the created
      type, plus
      possibly a handful of interfaces.

      | I would gladly give up the compile-time errors if I could get rid of
      | all these CType()s :)
      ?? CType doesn't help with have a variable of System.Type per se. However
      Generics should reduce or elimate the "need" for CType.

      FWIW: This is a good FAQ on Generics:



      --
      Hope this helps
      Jay [MVP - Outlook]
      ..NET Application Architect, Enthusiast, & Evangelist
      T.S. Bradley - http://www.tsbradley.net


      "Gazarsgo" <gazarsgo@gmail .com> wrote in message
      news:1138205218 .213763.113880@ o13g2000cwo.goo glegroups.com.. .
      | This seems to be a bit of a contradiction, but how can I construct a
      | Generic class using a System.Type variable that is assigned at run
      | time? Is there some parallel to the Generics concept that extends to
      | having strictly-typed classes at run-time?
      |
      | I would gladly give up the compile-time errors if I could get rid of
      | all these CType()s :)
      |


      Comment

      • Cor Ligthert [MVP]

        #4
        Re: using Generics dynamically (?)

        Gazargsgo,

        Never tried and never will, however are you not just looking at "object".

        Cor


        Comment

        • Gazarsgo

          #5
          Re: using Generics dynamically (?)


          Cor Ligthert [MVP] wrote:[color=blue]
          > Never tried and never will, however are you not just looking at "object".[/color]

          I am trying to implement an alternative to Object references using
          Generics, so as to have my class functions return the specific type
          rather than an Object reference, and to avoid boxing where possible.


          Herfried K. Wagner [MVP] wrote:[color=blue]
          > Strict typing is a compile-time feature.[/color]

          Maybe this is my own ignorance, but by "strict typing" I meant to refer
          to type safety, whether implemented via static typing at compile time
          or dynamic typing at runtime.


          It looks like Type.MakeGeneri cType and Activator.Creat eInstance are
          exactly what I was looking for, thanks for the extended code snippet
          Jay.

          Comment

          Working...