I have a generic Class I'm using to hold information loaded from a database.
I have a method which takes a DataRow as an argument, uses the object's known column name and extracts the data from the DataRow, such that:
Dim loadData As T = CType(myDataRow("myColumnName"), T))
works as my default assignment in most cases.
Unfortunately, due to some horrifying design constraints, some of my columns may be null, and may also be taken from enumerations.
This means that when <T>
is Nullable(Of SomeEnumeration)
the above code does not work because I can't cast 0
directly to SomeEnumeration.Zero
.
Is there some way to check whether <T>
is Nullable(Of [Enum])
? Or some way to write a method which allows Integers
to be cast to Nullable(Of [Enum])
?
I feel like I'm forgetting something that would allow me to write one of the other of these, but my weak google-fu is turning up nothing.
EDIT: Okay, thanks to dasblinkenlight's answer below, I can detect when this circumstance is occurring, but what I need to do now is to take a type <T>
which I know is Nullable(Of SomeClass)
, get a type reference to SomeClass
and then create a new object of type Nullable(Of SomeClass)
and assign that to LoadData.
Everything I try falls apart. Any ideas?
EDIT 2:
My solution for getting the value of that enum value was:
Dim baseType As Type = Nullable.GetUnderlyingType(GetType(T))
loadData = [Enum].Parse(baseType, dataRowIn(Me._dataName))
My problem was that I had a lot of difficulty in finding any function which would accept baseType
as an actual Type.
[Enum].Parse accepted baseType
as a parameter, I knew baseType
was an [Enum] type because of dasblinkenlight's code, so I was, in this instance, able to code a solution. It's a solution which is very specific to my problem (i.e., T
is Nullable(of SomeEnumeration)
), but it's a solution nonetheless.