Changing Type
Sometimes you need to change objects type to another type.Convert.ChangeType does this job for you. But when you use Nullable(Of T), this function throws an exception.You can use the following function to change type(including Nullable types) safely... Private Function CType2(ByVal...
Sometimes you need to change objects type to another type.Convert.ChangeType does this job for you. But when you use Nullable(Of T), this function throws an exception.
You can use the following function to change type(including Nullable types) safely...
Private Function CType2(ByVal refObject As Object, ByVal destType As System.Type) As Object If destType Is Nothing Then Throw New ArgumentException("Destination Type is null") If refObject.GetType() Is destType Then Return refObject If destType.IsGenericType And destType.GetGenericTypeDefinition().Equals(GetType(Nullable(Of ))) Then If refObject Is Nothing Then Return Nothing Dim c As New System.ComponentModel.NullableConverter(destType) destType = c.UnderlyingType End If Return Convert.ChangeType(refObject, destType) End Function