How to make a generic number parser in C#?

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).


Yes, the types that can be parsed from a string will most likely have static Parse and TryParse overloads that you can find via reflection like Jon suggested.

private static Func<string, T> GetParser<T>()
{
    // The method we are searching for accepts a single string.
    // You can add other types, like IFormatProvider to target specific overloads.
    var signature = new[] { typeof(string) };

    // Get the method with the specified name and parameters.
    var method = typeof(T).GetMethod("Parse", signature);

    // Initialize the parser delegate.
    return s => (T)method.Invoke(null, new[] { s });
}

Performance-wise - Invoke method accepts the method parameters as an object[] which is an unnecessary allocation and if your parameters include value types, causes boxing. It also returns an object, causing the resulting number (assuming it's a value type as well) to be boxed.

To counter that, you can build lambda expressions:

private static Func<string, T> GetParser<T>()
{
    // Get the method like we did before.
    var signature = new[] { typeof(string) };
    var method = typeof(T).GetMethod("Parse", signature);

    // Build and compile a lambda expression.
    var param = Expression.Parameter(typeof(string));
    var call = Expression.Call(method, param);
    var lambda = Expression.Lambda<Func<string, T>>(call, param);
    return lambda.Compile();
}

Calling the compiled lambda expression is virtually 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.

private static class Parser<T>
{
    public static readonly Func<string, T> Parse = InitParser();

    private static Func<string, T> InitParser()
    {
        // Our initialization logic above.
    }
}

After that your parsing method can be written like this:

public static T Parse<T>(string s)
{
    return Parser<T>.Parse(s);
}

This is very hackish, but it works using Newtonsoft.Json (Json.NET):

 JsonConvert.DeserializeObject<double>("24.11");
 // Type == System.Double - Value: 24.11

 JsonConvert.DeserializeObject<int>("29.4");
 // Type == System.Int32 - Value: 29

You'd basically have to use reflection to find the relevant static Parse method, invoke it, and cast the return value back to T. Alternatively, you could use Convert.ChangeType or get the relevant TypeDescriptor and associated TypeConverter.

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 from string. Another type could implement IConvertible without having any way of converting to that type from a string.