예제 #1
0
        /// <summary>
        /// 字符串与枚举型、TimeSpan与整型之间转换的方法。
        /// </summary>
        /// <param name="srcType">源数据类型</param>
        /// <param name="srcValue">源数据的值</param>
        /// <param name="targetType">目标数据类型</param>
        /// <returns>类型转换后的结果</returns>
        /// <remarks>字符串与枚举型、TimeSpan与整型之间转换的方法。
        /// <seealso cref="MCS.Library.Core.XmlHelper"/>
        /// </remarks>
        public static object ChangeType(System.Type srcType, object srcValue, System.Type targetType)
        {
            ExceptionHelper.FalseThrow <ArgumentNullException>(targetType != null, "targetType");
            bool   dealed = false;
            object result = null;

            if (srcType == typeof(object))
            {
                if (srcValue != null)
                {
                    srcType = srcValue.GetType();
                }
            }

            if (srcType == targetType)
            {
                result = srcValue;
                dealed = true;
            }
            else
            if (targetType == typeof(object))
            {
                result = srcValue;
                dealed = true;
            }
            else
            if (targetType.IsEnum)
            {
                if (srcType == typeof(string) || srcType == typeof(int) || srcType == typeof(long))
                {
                    if (srcValue is string && (srcValue).ToString().IsNullOrEmpty())
                    {
                        result = Enum.Parse(targetType, "0");
                    }
                    else
                    {
                        result = Enum.Parse(targetType, srcValue.ToString());
                    }

                    dealed = true;
                }
            }
            else
            if (targetType == typeof(string) && srcType == typeof(DateTime))
            {
                result = string.Format("{0:yyyy-MM-ddTHH:mm:ss.fff}", srcValue);

                dealed = true;
            }
            else
            if (targetType == typeof(TimeSpan))
            {
                if (srcType == typeof(TimeSpan))
                {
                    result = srcValue;
                }
                else
                {
                    result = TimeSpan.FromSeconds((double)Convert.ChangeType(srcValue, typeof(double)));
                }

                dealed = true;
            }
            else
            if (targetType == typeof(bool) && srcType == typeof(string))
            {
                result = StringToBool(srcValue.ToString(), out dealed);
            }
            else
            if (targetType == typeof(DateTime))
            {
                if (srcType == typeof(string))
                {
                    if (srcValue == null || srcValue.ToString() == string.Empty)
                    {
                        result = DateTime.MinValue;
                        dealed = true;
                    }
                }
                else
                if (typeof(IDictionary <string, object>).IsAssignableFrom(srcType))
                {
                    result = ((IDictionary <string, object>)srcValue).ToDateTime();
                    dealed = true;
                }
            }
            else
            if ((typeof(IDictionary <string, object>).IsAssignableFrom(targetType)) && srcType == typeof(DateTime))
            {
                result = ((DateTime)srcValue).ToDictionary();
                dealed = true;
            }

            if (dealed == false)
            {
                if (targetType != typeof(object) && targetType.IsAssignableFrom(srcType))
                {
                    result = srcValue;
                }
                else if (targetType == typeof(DateTime))
                {
                    result = Convert.ToDateTime(srcValue);
                }
                else
                {
                    result = Convert.ChangeType(srcValue, targetType);
                }
            }

            return(result);
        }