There's not a lot of point being generic if end up doing switch if/switch 
statements. ie Why don't you just have a specialized class for each numeric 
type? Ie IntFooClass, DoubleFooClass, etc.


From: ozdotnet-boun...@ozdotnet.com [mailto:ozdotnet-boun...@ozdotnet.com] On 
Behalf Of Geoff Appleby
Sent: Friday, June 03, 2011 8:59 PM
To: ozDotNet
Subject: Re: Generic class question

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 
<g...@mira.net<mailto:g...@mira.net>> 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