TypeConverters!

TypeConverters were introduced specifically for what (I think) you're trying
to do.  Here's some vague, hand-wavy, uncompiled code that should do the
trick.

string key = part.Split('=')[0];
// Not sure where you're getting 'value' from
PropertyDescriptorCollection propertyDescriptors =
TypeDescriptor.GetProperties (result.GetType ());
PropertyDescriptor property = propertyDescriptors [key];
property.SetValue (result, property.Converter.ConvertFrom (value));

Cheers,

                        Geoff

> -----Original Message-----
> From: Discussion of advanced .NET topics. [mailto:ADVANCED-
> [EMAIL PROTECTED] On Behalf Of Paul Cowan
> Sent: 18 July 2007 09:22
> To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM
> Subject: [ADVANCED-DOTNET] Type casting
>
> 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 DevelopMentorR  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