您的位置:首页 > 编程语言 > C#

C# 将String类型转化成任意类型

2016-10-27 13:41 295 查看
        #region 将字符串格式化成指定的数据类型

        /// <summary>

        /// 将字符串格式化成指定的数据类型

        /// </summary>

        /// <param name="str"></param>

        /// <param name="type"></param>

        /// <returns></returns>

        public static Object FormatString(this String str, Type type)

        {

            if (String.IsNullOrEmpty(str))

                return null;

            if (type == null)

                return str;

            if (type.IsArray)

            {

                Type elementType = type.GetElementType();

                String[] strs = str.Split(new char[] { ';' });

                Array array = Array.CreateInstance(elementType, strs.Length);

                for (int i = 0, c = strs.Length; i < c; ++i)

                {

                    array.SetValue(ConvertSimpleType(strs[i], elementType), i);

                }

                return array;

            }

            return ConvertSimpleType(str, type);

        }

        private static object ConvertSimpleType(object value, Type destinationType)

        {

            object returnValue;

            if ((value == null) || destinationType.IsInstanceOfType(value))

            {

                return value;

            }

            string str = value as string;

            if ((str != null) && (str.Length == 0))

            {

                return null;

            }

            TypeConverter converter = TypeDescriptor.GetConverter(destinationType);

            bool flag = converter.CanConvertFrom(value.GetType());

            if (!flag)

            {

                converter = TypeDescriptor.GetConverter(value.GetType());

            }

            if (!flag && !converter.CanConvertTo(destinationType))

            {

                throw new InvalidOperationException("无法转换成类型:" + value.ToString() + "==>" + destinationType);

            }

            try

            {

                returnValue = flag ? converter.ConvertFrom(null, null, value) : converter.ConvertTo(null, null, value, destinationType);

            }

            catch (Exception e)

            {

                throw new InvalidOperationException("类型转换出错:" + value.ToString() + "==>" + destinationType, e);

            }

            return returnValue;

        } 

        #endregion
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: