/// <summary>
/// 泛型类型转换
/// </summary>
/// <typeparam name="T">要转换的基础类型</typeparam>
/// <param name="val">要转换的值</param>
/// <returns></returns>
/// <remark>
/// by mcjiffy.cn
/// </remark>
public static T ConvertType<T>(object val)
{
if (val == null) return default(T);//返回类型的默认值
Type tp = typeof(T);
//泛型Nullable判断,取其中的类型
if (tp.IsGenericType)
{
tp = tp.GetGenericArguments()[0];
}
//string直接返回转换
if (tp.Name.ToLower() == "string")
{
return (T)val;
}
//反射获取TryParse方法
var TryParse = tp.GetMethod("TryParse", BindingFlags.Public | BindingFlags.Static, Type.DefaultBinder,
new Type[] { typeof(string), tp.MakeByRefType() },
new ParameterModifier[] { new ParameterModifier(2) });
var parameters = new object[] { val, Activator.CreateInstance(tp) };
bool success = (bool)TryParse.Invoke(null, parameters);
//成功返回转换后的值,否则返回类型的默认值
if (success)
{
return (T)parameters[1];
}
return default(T);
}