Re: Dynamic type conversion
- From: "smc750 via DotNetMonster.com" <u21663@uwe>
- Date: Fri, 19 May 2006 03:10:51 GMT
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
mark.olszowka@xxxxxxxxx wrote:
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
.
- References:
- Dynamic type conversion
- From: mark . olszowka
- Dynamic type conversion
- Prev by Date: RE: Default Methods created with Drag & Drop attempt to edit Prima
- Next by Date: Re: Email classes for .Net
- Previous by thread: Dynamic type conversion
- Next by thread: Re: Dynamic type conversion
- Index(es):
Relevant Pages
|