Generic Class: Passing Data Type to Placeholder

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Etienne-Louis Nicolet

    Generic Class: Passing Data Type to Placeholder

    I have a generic class defined as follows:

    Public Class SqlArgument(Of TDataType)

    Private _values as List(Of TDataType)

    Public Property Values() As List(Of TDataType)
    Get
    Return _values
    End Get
    Set(ByVal pValues As List(Of TDataType))
    _values = pValues
    End Set

    Public Sub New()
    Me.Values = New List(Of TDataType)
    End Sub

    ....

    End Class


    Given the DataType property of a (strongly typed) DataTable I'd like to
    create a new instance of SqlArgument with the data type of the appropriate
    table column. I thought about something like

    Sub TestArgument(pC olumn as System.Data.Dat aColumn)
    ...
    Dim s As new SqlArgument(Of pColumn.DataTyp e)
    ...
    End Sub

    but obviously this does not work. Can anybody give me a hint on how to do
    this?

    Many thanks for your suggestions,
    Etienne


  • Mattias Sjögren

    #2
    Re: Generic Class: Passing Data Type to Placeholder

    >Given the DataType property of a (strongly typed) DataTable I'd like to
    >create a new instance of SqlArgument with the data type of the appropriate
    >table column. I thought about something like
    >
    >Sub TestArgument(pC olumn as System.Data.Dat aColumn)
    ...
    Dim s As new SqlArgument(Of pColumn.DataTyp e)
    ...
    >End Sub
    >
    >but obviously this does not work. Can anybody give me a hint on how to do
    >this?
    You have to go the Reflection route if you want to do this. You can
    instantiate the right generic type using Type.MakeGeneri cType and then
    an object from that type with Activator.Creat eInstance. But know that
    once you've started using Reflection, it's hard to do something
    without it, and the code quickly gets pretty ugly.

    What are you actually going to do with the s variable once you've
    created the instance?


    Mattias

    --
    Mattias Sjögren [C# MVP] mattias @ mvps.org
    http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
    Please reply only to the newsgroup.

    Comment

    • Marc Gravell

      #3
      Re: Generic Class: Passing Data Type to Placeholder

      Can I just say: look at LINQ-to-SQL; that is exactly what it does for
      you. You might be able to save a lot of effort...

      Marc

      Comment

      • Etienne-Louis Nicolet

        #4
        Re: Generic Class: Passing Data Type to Placeholder

        Dear Marc,

        I bought a book on LINQ yesterday and will have a look at it. Thanks for the
        tip!

        Kind regards,
        Etienne

        "Marc Gravell" <marc.gravell@g mail.comwrote in message
        news:%23$TZr1H% 23IHA.2232@TK2M SFTNGP05.phx.gb l...
        Can I just say: look at LINQ-to-SQL; that is exactly what it does for you.
        You might be able to save a lot of effort...
        >
        Marc

        Comment

        • Etienne-Louis Nicolet

          #5
          Re: Generic Class: Passing Data Type to Placeholder


          "Mattias Sjögren" <mattias.dont.w ant.spam@mvps.o rgwrote in message
          news:eftinBH%23 IHA.3656@TK2MSF TNGP03.phx.gbl. ..
          Given the DataType property of a (strongly typed) DataTable I'd like to
          >>create a new instance of SqlArgument with the data type of the appropriate
          >>table column. I thought about something like
          >>
          >>Sub TestArgument(pC olumn as System.Data.Dat aColumn)
          > ...
          > Dim s As new SqlArgument(Of pColumn.DataTyp e)
          > ...
          >>End Sub
          >>
          >>but obviously this does not work. Can anybody give me a hint on how to do
          >>this?
          >
          You have to go the Reflection route if you want to do this. You can
          instantiate the right generic type using Type.MakeGeneri cType and then
          an object from that type with Activator.Creat eInstance. But know that
          once you've started using Reflection, it's hard to do something
          without it, and the code quickly gets pretty ugly.
          >
          What are you actually going to do with the s variable once you've
          created the instance?
          >
          >
          Mattias
          >
          --
          Mattias Sjögren [C# MVP] mattias @ mvps.org
          http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
          Please reply only to the newsgroup.
          Dear Mattias,

          I tried to figure out how to use Reflection as recommended. I think i got
          the first part:

          Sub TestArgument(pC olumn as System.Data.Dat aColumn)
          Dim generic As System.Type = GetType(SqlArgu ment(Of ))
          Dim typeArgs() As Type = {pColumn.DataTy pe}
          Dim constructed As Type = generic.MakeGen ericType(typeAr gs)
          ....
          End Sub
          However, I did not get along with the VB help on Activator.Creat eInstance.
          Would you mind giving me a hint how the code should look like?

          Many thanks & kind regards,
          Etienne


          Comment

          Working...