/// <summary> /// /// </summary> /// <param name="targetType"></param> /// <param name="val"></param> /// <returns></returns> public static object ChangeType(Type targetType, object val) { if (val == null) { return(null); } if (targetType.IsAssignableFrom(val.GetType())) { return(val); } if (targetType == val.GetType()) { return(val); } if (targetType == typeof(bool)) { if (val.ToString() == "0") { return(false); } if (val.ToString() == "1") { return(true); } } if (targetType.IsEnum) { int result = 0; if (!int.TryParse(val.ToString(), out result)) { return(System.Enum.Parse(targetType, val.ToString())); } return(val); } if (targetType == typeof(Type)) { return(ReflectionConvertHelper.GetType(val.ToString())); } if (targetType == typeof(IComparable)) { return(val); } if (val == null || val.ToString() == "") { return(GetDefaultValue(targetType)); } return(Convert.ChangeType(val, targetType)); }
/// <summary> /// /// </summary> /// <param name="regularName"></param> /// <returns></returns> public static Type GetTypeByRegularName(string regularName) { return(ReflectionConvertHelper.GetType(regularName)); }