![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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(pColumn as System.Data.DataColumn) ... Dim s As new SqlArgument(Of pColumn.DataType) ... 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 |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Generic Class: Passing Data Type to Placeholder >Given the DataType property of a (strongly typed) DataTable I'd like to Quote: >create a new instance of SqlArgument with the data type of the appropriate >table column. I thought about something like > >Sub TestArgument(pColumn as System.Data.DataColumn) > ... > Dim s As new SqlArgument(Of pColumn.DataType) > ... >End Sub > >but obviously this does not work. Can anybody give me a hint on how to do >this? instantiate the right generic type using Type.MakeGenericType and then an object from that type with Activator.CreateInstance. 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. |
My System Specs![]() |
| | #3 (permalink) |
| | 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 |
My System Specs![]() |
| | #4 (permalink) |
| | 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@xxxxxx> wrote in message news:%23$TZr1H%23IHA.2232@xxxxxx Quote: > 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 |
My System Specs![]() |
| | #5 (permalink) |
| | Re: Generic Class: Passing Data Type to Placeholder "Mattias Sjögren" <mattias.dont.want.spam@xxxxxx> wrote in message news:eftinBH%23IHA.3656@xxxxxx Quote: Quote: > >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(pColumn as System.Data.DataColumn) >> ... >> Dim s As new SqlArgument(Of pColumn.DataType) >> ... >>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.MakeGenericType and then > an object from that type with Activator.CreateInstance. 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. I tried to figure out how to use Reflection as recommended. I think i got the first part: Sub TestArgument(pColumn as System.Data.DataColumn) Dim generic As System.Type = GetType(SqlArgument(Of )) Dim typeArgs() As Type = {pColumn.DataType} Dim constructed As Type = generic.MakeGenericType(typeArgs) .... End Sub However, I did not get along with the VB help on Activator.CreateInstance. Would you mind giving me a hint how the code should look like? Many thanks & kind regards, Etienne |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Is StringBuilder class Value type? | .NET General | |||
| Passing credential object - what's the type? | PowerShell | |||
| Re: Unable to cast COM object of type 'ADODB.RecordsetClass' to class type 'System.Object[]' | .NET General | |||
| How to Create Collection Generic Class using new-object | PowerShell | |||