Dynamic type conversion

M

mark.olszowka

I am writing a generic property table as part of my application

property_name
property_type
property_value

All information is stored in the database as strings.

In my application that maintains this data, I want to ensure that the
user cannot type in a value that cannot be converted to the specified
property_type. There is no list of allowable property types. Any type
or enum that exists in the .Net Framework, or a custom type or enum
derived from the .Net framework is fair game in my application.

I can't seem to figure out how to verify this.

For example:

property_name = "RadiobuttonList.RepeatLayout"
property_type = "System.Web.UI.WebControls.RepeatLayout"
property_value = "Flow"

What code can I write to verify that "Flow" can be converted to the
type specified by the string "System.Web.UI.WebControls.RepeatLayout"?

Here is some of the code that I have tried

Dim strPropertyType As String
Dim strPropertyValue As String
Dim objType As System.Type

strPropertyValue = "Flow"
strPropertyType = "System.Web.UI.WebControls.RepeatLayout"

objType = System.Type.GetType(strPropertyType)
Try
System.Convert.ChangeType(strPropertyValue, objType)
Catch objException As System.Exception
ErrorMessage = "Invalid Property value specified"
End Try

This seems to work for types like String, Int32, etc, but not for Enums
(at least not for System.Web.UI.WebControls.RepeatLayout).

Help!

Remember, at design time, I know NOTHING about the type information I
will be working with.

Thanks

Mark Olszowka
 
S

smc750 via DotNetMonster.com

Unfortunately, there is much more to dynamic conversion in the .Net
Framework.
The Convert class only supports "base types" which msdn lists as Int64,
boolean etc (value types). The method you have listed won't work on
enumerations. Enumerations are considered a special form of a value type,
which has a name, an underlying type and a set of fields. What you need is a
little bit of reflection with the System.Reflection namespace.

For instance, you need to devise a coding strategy to interrogate the type
and different ways of validating the value. In the case of enumerations you
could check the type to see if it is an enumeration

example: type.IsEnum

Once you have determined if the type is an enumeration you could validate the
value this way.
This assumes the value is a string which can be converted to a Int32, byte,
UInt64. This is also assuming the user of your class is passing in the
underlying type and not the string representation of the name of the
enumeration.

Dim fieldinfo() As FieldInfo = type.GetFields

For x = 1 To fieldinfo.GetUpperBound(0)
if fieldinfo(x).GetValue(fieldinfo(x)) = value then
valid = true
end if
next

If your type is a value type and not an enumeration you can use the code you
have listed.
You can check to see if the type is a value type by using type.IsValueType.

Now there also might be a problem with using just strings for the values. Not
all "objects" have string representations that can be converted. If you
somehow limit the use of your class to value types and possibly enumerations
this all may work.

Hope this helps.

smc750
http://www.certdev.com





I am writing a generic property table as part of my application

property_name
property_type
property_value

All information is stored in the database as strings.

In my application that maintains this data, I want to ensure that the
user cannot type in a value that cannot be converted to the specified
property_type. There is no list of allowable property types. Any type
or enum that exists in the .Net Framework, or a custom type or enum
derived from the .Net framework is fair game in my application.

I can't seem to figure out how to verify this.

For example:

property_name = "RadiobuttonList.RepeatLayout"
property_type = "System.Web.UI.WebControls.RepeatLayout"
property_value = "Flow"

What code can I write to verify that "Flow" can be converted to the
type specified by the string "System.Web.UI.WebControls.RepeatLayout"?

Here is some of the code that I have tried

Dim strPropertyType As String
Dim strPropertyValue As String
Dim objType As System.Type

strPropertyValue = "Flow"
strPropertyType = "System.Web.UI.WebControls.RepeatLayout"

objType = System.Type.GetType(strPropertyType)
Try
System.Convert.ChangeType(strPropertyValue, objType)
Catch objException As System.Exception
ErrorMessage = "Invalid Property value specified"
End Try

This seems to work for types like String, Int32, etc, but not for Enums
(at least not for System.Web.UI.WebControls.RepeatLayout).

Help!

Remember, at design time, I know NOTHING about the type information I
will be working with.

Thanks

Mark Olszowka

--
smc750
www.certdev.com

Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/Forums.aspx/dotnet-general/200605/1
 
G

Greg Young

For enums you can use Enum.Parse() which will convert it for you .. you can
also use Enum.GetValues() if you want to view the available items (say for
displaying a drop down for the user to choose from).

Cheers,

Greg Young
MVP - C#
 
M

mark.olszowka

The problem I am having is getting

objType = System.Type.GetType(strPropertyType)

To execute successfully when strPropertyType is the string
representation of an enum, such as
System.Web.UI.WebControls.RepeatLayout. It errors on the GetType, and
therefore I do not have a type to call IsEnum on to determine if the
string representation represents an enum or not.

Can you get this line to work? Even using a different enum from the
framework?
 
S

smc750 via DotNetMonster.com

I was able to get this to work with System.IO.FileAccess and System.DayOfWeek.
However, others will not work. Why is beyond me at this point. This is very
inconsistent behavior. There does not seem to be any pattern on why some
enumerations will work and others don't.

Sorry I could not help on this. I will let you know if I find anything out.

smc750
http://www.certdev.com
 

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