Hi Paul,

System.ComponentModel.TypeConverter and TypeDescriptor types could help
you convert a string to a given type. Here's a possible - not optimized
- replacement of the code you have posted:

string key = part.Split('=')[0];
PropertyInfo property = result.GetType().GetProperty(key);

property.SetValue(result,

TypeDescriptor.GetConverter(property.PropertyType).ConvertFromString(value),
   null);

Hope this helps.

--
Efran Cobisi
http://www.cobisi.com

Paul Cowan wrote:
Hi,

I have the following piece of code:

string key = part.Split('=')[0];
PropertyInfo property = result.GetType().GetProperty(key);
if (property.PropertyType == typeof(string))
     property.SetValue(result, value, null);
else if (property.PropertyType == typeof(Int32))
    property.SetValue(result, Int32.Parse(value), null);
else if (property.PropertyType == typeof(bool))
    property.SetValue(result, bool.Parse(value), null);

My question is this:

Is there a way of generically typecasting the property value and not having to 
add an extra else if for each different type that the value might be.
Thanks
Paul
[EMAIL PROTECTED]
_________________________________________________________________
Celeb spotting – Play CelebMashup and win cool prizes
https://www.celebmashup.com/index2.html
===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com


===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to