To parse a string to an int, one calls Int32.Parse(string)
, for double, Double.Parse(string)
, for long, Int64.Parse(string)
, and so on..
Is it possible to create a method that makes it generic, for example, ParseString<T>(string)
? where T
can be Int32
, Double
, etc. I notice the number of types don't implement any common interface, and the Parse
methods don't have any common parent.
Is there any way to achieve this or something similar to this?
You'd basically have to use reflection to find the relevant static
Parse
method, invoke it, and cast the return value back toT
. Alternatively, you could useConvert.ChangeType
or get the relevantTypeDescriptor
and associatedTypeConverter
.A more limited but efficient (and simple, in some ways) approach would be to keep a dictionary from type to parsing delegate - cast the delegate to a
Func<string, T>
and invoke it. That would allow you to use different methods for different types, but you'd need to know the types you needed to convert to up-front.Whatever you do, you won't be able to specify a generic constraint which would make it safe at compile-time though. Really you need something like my idea of static interfaces for that kind of thing. EDIT: As mentioned, there's the
IConvertible
interface, but that doesn't necessarily mean that you'll be able to convert fromstring
. Another type could implementIConvertible
without having any way of converting to that type from a string.This is very hackish, but it works using Newtonsoft.Json (Json.NET):
Actually, the standard number types do implement a common interface: IConvertible. This is the one that
Convert.ChangeType
use.Unfortunately, there is no
TryParse
equivalent, it will throw exceptions if the string cannot be parsed.As a side note, it seems this whole "conversion" area has been completely forgotten by the BCL team. There is nothing new there since .NET Framework 1 (except from TryParse methods).
I have written some code that uses reflection to find
Parse
/TryParse
methods on a type and access these from generic functions:https://github.com/CodesInChaos/ChaosUtil/blob/master/Chaos.Util/Conversion.cs
But I haven't tested them too much, so they might stiff have some bugs/not work correctly with every type. The error handling is a bit lacking too.
And they have no overloads for culture invariant parsing. So you probably need to add that.
Yes, the types that can be parsed from a string will most likely have static
Parse
andTryParse
overloads that you can find via reflection like Jon suggested.For performance you can also build lambda expressions calling them since the
Invoke
method accepts the method parameters as anobject[]
which is an unnecessary allocation and if your parameters include value types, causes boxing. It also returns the result as anobject
which also causes boxing when your type is a value type.Calling the compiled lambda expression is essentially as fast as calling the original parsing method itself, but building and compiling it in the first place is not. This is why, again like Jon suggested, we should cache the resulting delegate.
I use a static, generic class to cache parsers in ValueString.
After that your parsing method can be written like this: