"Mattias Sjögren" <mattias.dont.want.spam@xxxxxx> wrote in message
news:eftinBH%23IHA.3656@xxxxxx
> >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. Dear Mattias,
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