Dynamic TryParse for all data types

别说谁变了你拦得住时间么 提交于 2019-11-29 16:31:56

You should use the TypeDescriptor class:

public static T Convert<T>(this string input)
{
    var converter = TypeDescriptor.GetConverter(typeof(T));
    if(converter != null)
    {
        //Cast ConvertFromString(string text) : object to (T)
        return (T)converter.ConvertFromString(input);
    }
    return default(T);
}

of course this will throw an exception if the conversion fails so you will want to try/catch it.

Here is my version of generic TryParse method. I believe you can use this version too:

double pi;
if(ValueTypeHelper.TryParse("3.14159", out pi)) {
    // .. pi = 3.14159
}
...
string eStr = "2.71828";
float e;
if(eStr.TryParse(out e)) {
    // .. e = 2.71828f
}
...
static class ValueTypeHelper {
    static IDictionary<Type, Delegate> cache = new Dictionary<Type, Delegate>();
    public static bool TryParse<T>(this string valueStr, out T result) {
        Delegate d = null;
        if(!cache.TryGetValue(typeof(T), out d)) {
            var mInfos = typeof(T).GetMember("TryParse", MemberTypes.Method, BindingFlags.Static | BindingFlags.Public);
            if(mInfos.Length > 0) {
                var s = Expression.Parameter(typeof(string));
                var r = Expression.Parameter(typeof(T).MakeByRefType());
                d = Expression.Lambda<TryParseDelegate<T>>(
                    Expression.Call(mInfos[0] as MethodInfo, s, r), s, r).Compile();
            }
            cache.Add(typeof(T), d);
        }
        result = default(T);
        TryParseDelegate<T> tryParse = d as TryParseDelegate<T>;
        return (tryParse != null) && tryParse(valueStr, out result);
    }
    delegate bool TryParseDelegate<T>(string valueStr, out T result);
}

I have combined both DmitryG's and RezaRahmati's suggested solutions:

static class GenericValueConverter
{
    public static bool TryParse<T>(this string input, out T result)
    {
        bool isConversionSuccessful = false;
        result = default(T);

        var converter = TypeDescriptor.GetConverter(typeof(T));
        if (converter != null)
        {
            try
            {
                result = (T)converter.ConvertFromString(input);
                isConversionSuccessful = true;
            }
            catch { }
        }

        return isConversionSuccessful;
    }
}

void Main()
{
    double pi;
    if (GenericValueConverter.TryParse("3,14159", out pi)) //Use right decimal point seperator for local culture
    {
        pi.Dump(); //ConsoleWriteline for LinqPad
        //pi=3,14159
    }

    string dtStr = "2016-12-21T16:34:22";
    DateTime dt;
    if (dtStr.TryParse(out dt))
    {
        dt.Dump(); //ConsoleWriteline for LinqPad
        //dt=21.12.2016 16:34:22
    }

    string guidStr = "D430831B-03B0-44D5-A971-4E73AF96B5DF";
    Guid guid;
    if (guidStr.TryParse(out guid))
    {
        guid.Dump(); //ConsoleWriteline for LinqPad
        //guid=d430831b-03b0-44d5-a971-4e73af96b5df
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!