Parse a specified arithmetic type from string This class has the responsibility of parsing a string to one of the primitive types: int, double, float, decimal... 

While creating it, I have noticed that it'll be terribly ugly. 

It basically should perform a TryParse of each primitive type and return the first that returns true, along with the parsed value.

public static class PrimitiveParser
{
    public static bool TryParse(Type targetType, string sourceValue, out object result)
    {
        result = null;

        if (targetType == typeof(int))
        {
            int intResult;
            var parseResult = int.TryParse(sourceValue, out intResult);
            {
                result = intResult;
                return parseResult;
            }
        }
        if (targetType == typeof(double))
        {
            double doubleResult;
            var parseResult = double.TryParse(sourceValue, out doubleResult);
            {
                result = doubleResult;
                return parseResult;
            }
        }
        if (targetType == typeof(float))
        {
            float floatResult;
            var parseResult = float.TryParse(sourceValue, out floatResult);
            {
                result = floatResult;
                return parseResult;
            }
        }

        /// ...

        return false;
    }
}
I'd prefer to use a dictionary for this.  

private delegate bool TryParseDelegate&lt;T&gt;(string text, out T value);

private static readonly Dictionary&lt;Type, object&gt; TryParsers = new Dictionary&lt;Type, object&gt;
{
    { typeof(int), (TryParseDelegate&lt;int&gt;)int.TryParse },
    { typeof(float), (TryParseDelegate&lt;float&gt;)float.TryParse },
    { typeof(double), (TryParseDelegate&lt;double&gt;)double.TryParse }
};


The TryParse method:

public static bool TryParse&lt;T&gt;(string value, out T returnValue)
{
    // Check if we have a parse method for T in the dictionary.
    object parserObj;
    if (TryParsers.TryGetValue(typeof(T), out parserObj))
    {
        TryParseDelegate&lt;T&gt; parser = (TryParseDelegate&lt;T&gt;)parserObj;
        return parser(value, out returnValue);
    }
    // If no, fallback to IConvertible.
    if (typeof(IConvertible).IsAssignableFrom(typeof(T)))
    {
        try
        {
            returnValue = (T)Convert.ChangeType(value, typeof(T));
            return true;
        }
        catch { }
    }
    // No luck.
    returnValue = default(T);
    return false;
}




This approach support exensibility.
For example, imagine we have a class A:

public class A
{
    public static readonly A Zero = new A("Zero");
    public static readonly A One = new A("One");

    public readonly string Text;

    private A(string text)
    {
        Text = text;
    }

    public static bool TryParse(string text, out A value)
    {
        switch (text.ToLower())
        {
            case "zero":
                value = Zero;
                break;
            case "one":
                value = One;
                break;
            default:
                value = null;
                return false;
        }
        return true;
    }

    public override string ToString()
    {
        return Text;
    }
}


Then the only thing you have to do is to append an item to the dictionary:

{ typeof(A), (TryParseDelegate&lt;A&gt;)A.TryParse }


Let's test it:

int i;
double d;
A a;

if (TryParse("123", out i))
    Console.WriteLine($"i={i}"); // Prints i=123
if (TryParse("123.456", out d))
    Console.WriteLine($"d={d}"); // Prints d=123.456
if (TryParse("ONE", out a))
    Console.WriteLine($"a={a}"); // Prints a=One