本文介绍了适用于所有数据类型的动态TryParse的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检查对象是否可以转换为特定的DataType,并提出以下建议:

I have the need to examine to see if an object can be converted to a specific DataType or not, and came up with this :

public static bool TryParseAll(System.Type typeToConvert, object valueToConvert)
{

    bool succeed = false;

    switch (typeToConvert.Name.ToUpper())
    {
        case "DOUBLE":
            double d;
            succeed = double.TryParse(valueToConvert.ToString(), out d);
            break;
        case "DATETIME":
            DateTime dt;
            succeed = DateTime.TryParse(valueToConvert.ToString(), out dt);
            break;
        case "INT16":
            Int16 i16;
            succeed = Int16.TryParse(valueToConvert.ToString(), out i16);
            break;
        case "INT":
            Int32 i32;
            succeed = Int32.TryParse(valueToConvert.ToString(), out i32);
            break;
        case "INT32":
            Int32 i322;
            succeed = Int32.TryParse(valueToConvert.ToString(), out i322);
            break;
        case "INT64":
            Int64 i64;
            succeed = Int64.TryParse(valueToConvert.ToString(), out i64);
            break;
        case "BOOLEAN":
            bool b;
            succeed = Boolean.TryParse(valueToConvert.ToString(), out b);
            break;
        case "BOOL":
            bool b1;
            succeed = bool.TryParse(valueToConvert.ToString(), out b1);
            break;
    }

    return succeed;
}

我想知道除此之外还有其他方法吗?哪个更动态,更高效?

I'm wondering is there any ways other than this? Which is more dynamic and more efficient?

谢谢!

推荐答案

这是我的通用TryParse方法版本.我相信您也可以使用此版本:

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);
}

这篇关于适用于所有数据类型的动态TryParse的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 02:31