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
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