/// <summary>
 /// 类型转换
 /// </summary>
 /// <param name="value"></param>
 /// <param name="destinationType"></param>
 /// <param name="culture"></param>
 /// <returns></returns>
 public static object ToObject(this object value, Type destinationType, CultureInfo culture)
 {
     if (value != null)
     {
         var           sourceType           = value.GetType();
         TypeConverter destinationConverter = CustomTypeConverterHelper.GetCustomTypeConverter(destinationType);
         TypeConverter sourceConverter      = CustomTypeConverterHelper.GetCustomTypeConverter(sourceType);
         if (destinationConverter != null && destinationConverter.CanConvertFrom(value.GetType()))
         {
             return(destinationConverter.ConvertFrom(null, culture, value));
         }
         if (sourceConverter != null && sourceConverter.CanConvertTo(destinationType))
         {
             return(sourceConverter.ConvertTo(null, culture, value, destinationType));
         }
         if (destinationType.IsEnum)
         {
             return(Enum.ToObject(destinationType, value));
         }
         if (!destinationType.IsAssignableFrom(value.GetType()))
         {
             return(Convert.ChangeType(value, destinationType, culture));
         }
     }
     return(value);
 }
        /// <summary>
        /// 类型转换
        /// </summary>
        /// <typeparam name="TInput">输入类型</typeparam>
        /// <typeparam name="TOutput">输出类型</typeparam>
        /// <param name="value">源值</param>
        /// <param name="defaultValue">转换失败时的默认值</param>
        /// <param name="isEnumDefined">是否枚举定义</param>
        /// <returns>转换结果</returns>
        public static TOutput To <TInput, TOutput>(this TInput value, TOutput defaultValue = default(TOutput), bool isEnumDefined = true)
        {
            try
            {
                var inputType  = typeof(TInput);
                var outputType = typeof(TOutput);

                if (inputType.IsValueType && outputType.IsValueType)
                {
                    // 判断泛型值是否为默认值
                    if (value is Nullable && EqualityComparer <TInput> .Default.Equals(value, default(TInput)))
                    {
                        return(defaultValue);
                    }
                    if (outputType.IsGenericType)
                    {
                        outputType = outputType.GenericTypeArguments.FirstOrDefault();
                    }
                    TypeConverter outputConverter = CustomTypeConverterHelper.GetCustomTypeConverter(outputType);
                    TypeConverter inputConverter  = CustomTypeConverterHelper.GetCustomTypeConverter(inputType);
                    if (outputConverter != null && outputConverter.CanConvertFrom(inputType))
                    {
                        return((TOutput)outputConverter.ConvertFrom(null, CultureInfo.InvariantCulture, value));
                    }
                    if (inputConverter != null && inputConverter.CanConvertTo(outputType))
                    {
                        return((TOutput)inputConverter.ConvertTo(null, CultureInfo.InvariantCulture, value, outputType));
                    }
                }
                if ((inputType.IsValueType && !inputType.IsEnum && !inputType.IsGenericType) ||
                    (inputType.IsGenericType && inputType.IsValueType && !inputType.GenericTypeArguments.FirstOrDefault().IsEnum))
                {
                    // 如果目标是枚举类型
                    if (outputType.IsEnum)
                    {
                        if ((isEnumDefined && Enum.IsDefined(outputType, value.To <TInput, int>())) || !isEnumDefined)
                        {
                            defaultValue = (TOutput)Enum.Parse(outputType, value.ConvertToString(), false);
                        }
                        return(defaultValue);
                    }
                    else if (typeof(bool).Equals(inputType) && typeof(System.Int32).Equals(outputType))
                    {
                        return(Convert.ToInt32(value).To <int, TOutput>());
                    }
                    else if (typeof(bool?).Equals(inputType) && typeof(System.Int32).Equals(outputType))
                    {
                        if ((value as bool?).HasValue)
                        {
                            return(Convert.ToInt32(value).To <int, TOutput>());
                        }
                        else
                        {
                            return(default(TOutput));
                        }
                    }

                    return(value.ConvertToString().To <TOutput>());
                }
                return((TOutput)value.ToObject(outputType, CultureInfo.InvariantCulture));
            }
            catch
            {
                return(defaultValue);
            }
        }