Surely there's very few types that a) you're using, that b) offer that Parse
method. Could you save the reflection call and simply do something like

if (typeof(T) is decimal) { decimal.Parse(...); }
elseif (typeof(T) is int) { int.Parse(...); }

?

On Sat, Jun 4, 2011 at 1:27 PM, Greg Keogh <[email protected]> wrote:

> The Convert class will help:
>
> value = (T)Convert.ChangeType(rawValue, typeof(T))
>
> Fascinating. I’ve not noticed that method before. The MSDN doco says that
> ChangeType expects an IConvertible, which int, double, long, etc do.
>
> It turns out I couldn’t use a TypeConverter because I’m in Silverlight.
>
> I can’t use ChangeType in my case either, but I will stick that in my head
> for future use. I have to parse strings that are coming in a decimal number
> strings and also as 0xnnnnn hex number strings. Sadly I had to use
> reflection to get the Parse method and invoke it on the string after I have
> tweaked it.
>
> Greg
>
>
>



-- 
Geoff Appleby

Blog: http://www.crankygoblin.com/geoff
Twitter: http://twitter.com/g_appleby
Facebook: http://www.facebook.com/geoff.appleby

Reply via email to