Type Conversion Questions

G

Guest

In my project, I am setting Options Strict on.
In one place, I have generic code to set values in my objects using a
configuration file, code which is trying to set properties of my classes
using Invokemember, using this code -

dim bindSETPROP As BindingFlags = BindingFlags.IgnoreCase Or
BindingFlags.Instance Or BindingFlags.Public Or BindingFlags.SetProperty
Me.GetType.InvokeMember(strPropertyName, bindSETPROP, Nothing, Me, objParams)

Where strPropertyName is the property being set. The data is packaged into
objParams.
As part of the config file, it specifies the data type, so the objParams
information contains the correct type when setting the property value, so for
example, a line from the config file would read in t
property name value datatype
"FirstName" "Frank" "String"

so the object would package "Frank" as a string, and set the FirstName of
the object using InvokeMember. This works great until I get to properties
defined as an Enum. The data in the file is Integer data, but if it is not
cast to the proper Enum, the INvokeMember fails, as I have Option Strict On.
I am trying to figure out a way to dynamically cast an integer to a specific
Enum type when I have the name of the Enum. CType will not work , as it needs
the type itself to be passed in, which I need to do dynamically. Apparently,
calling CType( intVal, type.GetType("eenumtype")) is not valid.

So to make a long story short, is there any way to do a dynamic cast when I
only have the type name? (Short of building a big case statement)?
Jonathan Steinberg
BRCorp
 
G

Guest

I thought of a workaround:

Public Shared Function EnumConv(ByVal intIN As Integer, ByVal strEnumName As
String) As Object

Dim arrVals As Array

'Enum Type is "MyProj.eFooEnum"

arrVals = System.Enum.GetValues(System.Type.GetType(strEnumName))
For Each objVal As Object In arrVals
If intIN = CInt(objVal ) Then
Return objVal
End If
Next

End Function
This function will return an Object containing an enum, which I can pass
into InvokeMethod, which works. Bit of a kludge, though.

JS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top