NotaClue
asked on
Custom CType Function That you define the "type" to convert to at runtime -- Anyone have an any ideas
I have wrtten a working solution that converts a datarow to a class, so I can manipulate it with the least pain, as I have to do this quite a bit I've written a base class to handle this for me that leverages reflection so it will work for any new classes I build. The code is below.
Public Sub FillClassFromDataSet(ByVal ds As DataSet, ByVal ClassToFill As Object, ByVal ClassType As Type)
'This function converts the information in a dataset to a class
'note for this method to be usable the properties of the class must match the
'column names in the dataset
Dim dc As System.Data.DataColumn
Dim dr As System.Data.DataRow
Try
'Get the data row to convert
dr = ds.Tables(0).Rows(0)
'For each column in our data row attempt to convert the value to class value
For Each dc In ds.Tables(0).Columns
'arglist is the object we use to pass the value to our class property
Dim arglist(0) As Object
'Get the system type of the class proeprty we are trying to convert to
Dim PropertyInfo As System.Reflection.Property Info = ClassType.GetProperty(dc.C olumnName)
'Check to make sure we have a corresponding property in the class to
'match our column name
If Not PropertyInfo Is Nothing Then
'based on the proprety type perform the appropriate conversion on the
'value we want to pass to the class property
Select Case PropertyInfo.PropertyType. ToString
Case "System.String" : arglist(0) = CType(dr(dc.Ordinal), System.String)
Case "System.Int16" : arglist(0) = CType(dr(dc.Ordinal), System.Int16)
Case "System.Int32" : arglist(0) = CType(dr(dc.Ordinal), System.Int32)
Case "System.Int64" : arglist(0) = CType(dr(dc.Ordinal), System.Int64)
Case "System.DateTime" : arglist(0) = CType(dr(dc.Ordinal), System.DateTime)
Case "System.Double" : arglist(0) = CType(dr(dc.Ordinal), System.Double)
Case "System.Decimal" : arglist(0) = CType(dr(dc.Ordinal), System.Decimal)
Case "System.Decimal" : arglist(0) = CType(dr(dc.Ordinal), System.Decimal)
Case Else
'An handled type
Throw New System.Exception("Unhandle d Property Type (" & PropertyInfo.PropertyType. ToString & ") in FillClassFormDataSet")
End Select
Try
'Now pass the value from the datacolumn to the correspoding class proprety
ClassType.InvokeMember(dc. ColumnName , Reflection.BindingFlags.Se tProperty, Nothing, ClassToFill, arglist)
Catch ex As Exception
Dim x As String = ex.Message
End Try
Else
Throw New System.Exception("Unknown Property (" & dc.ColumnName & ") in FillClassFormDataSet")
End If
Next
Catch ex As Exception
Console.WriteLine(ex.Messa ge)
End Try
End Sub
This code is ok but it only handles conversions for the types that I have defined in the class, int32, string and so on.
The problem is this I'd like to replace the whole select statement that gets the type of the property and does the appropriate conversion with the following lines
Dim PropertyType As Type = PropertyInfo.PropertyType
CType(dr(dc.Ordinal), PropertyType)
This way I could handle any/all types that could be safely stored in a db and won't have to worry about constantly updating my class
Of course the Ctype function only excepts system defined types and won't except my variable pointing to the type I'd like it converted to :(
Anyone know a way to get around this behaviour, I checked out the basetype for Ctype but it looks like evetually I'm still going to have the same problem and be back to my original verbose solution. Any ideas, help would be appreciated
Public Sub FillClassFromDataSet(ByVal
'This function converts the information in a dataset to a class
'note for this method to be usable the properties of the class must match the
'column names in the dataset
Dim dc As System.Data.DataColumn
Dim dr As System.Data.DataRow
Try
'Get the data row to convert
dr = ds.Tables(0).Rows(0)
'For each column in our data row attempt to convert the value to class value
For Each dc In ds.Tables(0).Columns
'arglist is the object we use to pass the value to our class property
Dim arglist(0) As Object
'Get the system type of the class proeprty we are trying to convert to
Dim PropertyInfo As System.Reflection.Property
'Check to make sure we have a corresponding property in the class to
'match our column name
If Not PropertyInfo Is Nothing Then
'based on the proprety type perform the appropriate conversion on the
'value we want to pass to the class property
Select Case PropertyInfo.PropertyType.
Case "System.String" : arglist(0) = CType(dr(dc.Ordinal), System.String)
Case "System.Int16" : arglist(0) = CType(dr(dc.Ordinal), System.Int16)
Case "System.Int32" : arglist(0) = CType(dr(dc.Ordinal), System.Int32)
Case "System.Int64" : arglist(0) = CType(dr(dc.Ordinal), System.Int64)
Case "System.DateTime" : arglist(0) = CType(dr(dc.Ordinal), System.DateTime)
Case "System.Double" : arglist(0) = CType(dr(dc.Ordinal), System.Double)
Case "System.Decimal" : arglist(0) = CType(dr(dc.Ordinal), System.Decimal)
Case "System.Decimal" : arglist(0) = CType(dr(dc.Ordinal), System.Decimal)
Case Else
'An handled type
Throw New System.Exception("Unhandle
End Select
Try
'Now pass the value from the datacolumn to the correspoding class proprety
ClassType.InvokeMember(dc.
Catch ex As Exception
Dim x As String = ex.Message
End Try
Else
Throw New System.Exception("Unknown Property (" & dc.ColumnName & ") in FillClassFormDataSet")
End If
Next
Catch ex As Exception
Console.WriteLine(ex.Messa
End Try
End Sub
This code is ok but it only handles conversions for the types that I have defined in the class, int32, string and so on.
The problem is this I'd like to replace the whole select statement that gets the type of the property and does the appropriate conversion with the following lines
Dim PropertyType As Type = PropertyInfo.PropertyType
CType(dr(dc.Ordinal), PropertyType)
This way I could handle any/all types that could be safely stored in a db and won't have to worry about constantly updating my class
Of course the Ctype function only excepts system defined types and won't except my variable pointing to the type I'd like it converted to :(
Anyone know a way to get around this behaviour, I checked out the basetype for Ctype but it looks like evetually I'm still going to have the same problem and be back to my original verbose solution. Any ideas, help would be appreciated
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Can you tell me why this question has been rated "B"?
ASKER
Exactly want I needed