|
-
Dec 17th, 2008, 12:26 AM
#1
Thread Starter
New Member
Custom type conversion
Hi all,
How do we convert a source type into a destination type before using reflection to assign value from source field to destination field in below code snippet:
Code:
Try
tempPropertyInfo = tempDestinationObject.GetType.GetProperty(sourceObjectName)
If Not tempPropertyInfo Is Nothing AndAlso sourceDataTable.Columns.Contains(sourceObjectName) AndAlso tempPropertyInfo.CanWrite Then
Select Case tempPropertyInfo.PropertyType.Namespace
Case "System"
tempPropertyValue = CObj(sourceDataTable.Rows(rowCount).Item(sourceObjectName))
If Not tempPropertyValue Is Nothing AndAlso tempPropertyInfo.PropertyType.FullName = "System.DateTime" Then
tempPropertyValue = CDate(tempPropertyValue)
End If
Case Else
tempPropertyValue = Activator.CreateInstance(tempPropertyInfo.PropertyType)
tempPropertyValue.Value = CObj(sourceDataTable.Rows(rowCount).Item(sourceObjectName))
End Select
If Not tempPropertyValue Is Nothing Then
tempPropertyInfo.SetValue(tempDestinationObject, tempPropertyValue, Nothing)
End If
End If
Catch ex As Exception
End Try
When I try to assign a boolean value to a custom data type, I get an unhandled excpetion here
Code:
tempPropertyValue.Value = CObj(sourceDataTable.Rows(rowCount).Item(sourceObjectName))
-
Dec 17th, 2008, 10:03 AM
#2
Re: Custom type conversion
What's the exception message? I notice that you are taking what you suggest might be a boolean, converting it to object, and assigning it to the Value member. You shouldn't need the CObj call, as Item will return an Object anyways, but is Value of type Object? If Value is of type boolean, then CBool would be the correct conversion. However, if Item(sourceObjectName) is not a Boolean, or is Null, that would cause problems, as well.
My usual boring signature: Nothing
 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|