M
Matt Porco
I am trying to figure out how to convert a primitive type (Integer,
Short, etc.) to a corresponding Enum with the same underlying type.
The problem is, I don't know the actual types or Enums at compile time
- only at runtime. I'm using a PropertyInfo object to reflect on a
property of Enum type, and I'm retrieving a number (Integer or
whatever) from a database, then trying to use PropertyInfo.SetValue to
set the value of the property to the number retrieved from the
database. Here's some code:
------------------------------------------------
Option Explicit On
Imports System.Reflection
Class TestClass
Enum TestType As Integer
TypeA = 0
TypeB = 1
TypeC = 2
End Enum
Dim _testField As TestType
Property TestProperty() As TestType
Get
Return _testField
End Get
Set(ByVal Value As TestType)
_testField = Value
End Set
End Property
Shared Function TestPropertyInfo() As PropertyInfo
Dim info As PropertyInfo
info = GetType(TestClass).GetProperty("TestProperty", _
BindingFlags.Instance Or BindingFlags.Public Or
BindingFlags.NonPublic)
Return info
End Function
End Class
Class TestHarness
Sub Main()
Dim obj As New TestClass
GetValueFromDatabase(obj)
End Sub
Sub GetValueFromDatabase(ByVal obj As TestClass)
' This code just builds a DataTable as an illustration - real code
' would query the database and retrieve results.
Dim table As New DataTable("Test")
table.Columns.Add(New DataColumn("IntegerColumn",
GetType(Integer)))
Dim row As DataRow = table.NewRow
row(0) = 212
' get value from retrieved results
Dim value As Object = row(0)
If value.Equals(DBNull.Value) Then
value = Nothing
End If
TestClass.TestPropertyInfo.SetValue(obj, value, Nothing)
' previous line throws the following exception:
' An unhandled exception of type 'System.ArgumentException'
occurred
' in mscorlib.dll
' Additional information: Object type cannot be converted to
target type.
End Sub
End Class
------------------------------------------------
Obviously it's not implicitly converting the Integer to a TestType
since I have Option Explicit On (and turning it off is not a
solution). So how can I convert the returned value's type (Integer)
to the property's type (TestType) at runtime? I tried using
Convert.ChangeType(Object, Type) to convert the returned value to the
type specified by PropertyInfo.PropertyType, but this still throws a
similar exception.
Thanks.
Matt ([email protected])
Short, etc.) to a corresponding Enum with the same underlying type.
The problem is, I don't know the actual types or Enums at compile time
- only at runtime. I'm using a PropertyInfo object to reflect on a
property of Enum type, and I'm retrieving a number (Integer or
whatever) from a database, then trying to use PropertyInfo.SetValue to
set the value of the property to the number retrieved from the
database. Here's some code:
------------------------------------------------
Option Explicit On
Imports System.Reflection
Class TestClass
Enum TestType As Integer
TypeA = 0
TypeB = 1
TypeC = 2
End Enum
Dim _testField As TestType
Property TestProperty() As TestType
Get
Return _testField
End Get
Set(ByVal Value As TestType)
_testField = Value
End Set
End Property
Shared Function TestPropertyInfo() As PropertyInfo
Dim info As PropertyInfo
info = GetType(TestClass).GetProperty("TestProperty", _
BindingFlags.Instance Or BindingFlags.Public Or
BindingFlags.NonPublic)
Return info
End Function
End Class
Class TestHarness
Sub Main()
Dim obj As New TestClass
GetValueFromDatabase(obj)
End Sub
Sub GetValueFromDatabase(ByVal obj As TestClass)
' This code just builds a DataTable as an illustration - real code
' would query the database and retrieve results.
Dim table As New DataTable("Test")
table.Columns.Add(New DataColumn("IntegerColumn",
GetType(Integer)))
Dim row As DataRow = table.NewRow
row(0) = 212
' get value from retrieved results
Dim value As Object = row(0)
If value.Equals(DBNull.Value) Then
value = Nothing
End If
TestClass.TestPropertyInfo.SetValue(obj, value, Nothing)
' previous line throws the following exception:
' An unhandled exception of type 'System.ArgumentException'
occurred
' in mscorlib.dll
' Additional information: Object type cannot be converted to
target type.
End Sub
End Class
------------------------------------------------
Obviously it's not implicitly converting the Integer to a TestType
since I have Option Explicit On (and turning it off is not a
solution). So how can I convert the returned value's type (Integer)
to the property's type (TestType) at runtime? I tried using
Convert.ChangeType(Object, Type) to convert the returned value to the
type specified by PropertyInfo.PropertyType, but this still throws a
similar exception.
Thanks.
Matt ([email protected])