Note: I am not trying to determing whether or not a generic parameter is nullable. What I want to know is, if it is nullable, what is the nullable's underlying type?
Here is what I am trying:
Function CreateTdParameter(Of T)(Name As String, Value As T) As TdParameter
Dim TdType As TdType
Dim ValueType As Type = GetType(T)
If ValueType.IsGenericType Then
Dim GenType As Type = ValueType.GetGenericTypeDefinition()
If GenType = GetType(Nullable(Of )) Then
ValueType = Activator.CreateInstance(GenType.MakeGenericType(New Type() {ValueType}))
ValueType = Nullable.GetUnderlyingType(ValueType.GetGenericTypeDefinition().UnderlyingSystemType)
End If
End If
If ValueType = GetType(String) Then
TdType = Teradata.Client.Provider.TdType.VarChar
ElseIf ValueType = GetType(Integer) Then
TdType = Teradata.Client.Provider.TdType.Integer
ElseIf ValueType = GetType(DateTime) Then
TdType = Teradata.Client.Provider.TdType.Timestamp
Else
Throw New NotImplementedException(String.Format("{0} not expected.", Value.GetType))
End If
Return CreateTdParameter(Name, Value, TdType, ParameterDirection.Input)
End Function
It results in the following error when a DateTime?
is passed through the Value
parameter:
GenericArguments[0], 'System.Nullable
1[System.DateTime]', on 'System.Nullable1[T]' violates the constraint of type 'T'.
It appears that I need to get the underlying type in order to instantiate a nullable of that type using reflection. Not sure how to get around this catch-22.
Can anyone point me to a way that I can tell whether or not a generic type is nullable and, if it is, how I can get its underlying type?