现在的位置: 首页 > 综合 > 正文

CollectionToObject

2014年01月28日 ⁄ 综合 ⁄ 共 2257字 ⁄ 字号 评论关闭
        #region CollectionToObject
        public static object CollectionToObject(NameValueCollection nvc, Object obj)
        {
            //object obj = Activator.CreateInstance( t );
            Type t = obj.GetType();
            PropertyInfo[] pis = t.GetProperties();
            for (int i = 0, len = pis.Length; i < len; i++)
            {
                // 转换时有两种异常情况需要处理
                // 一是 没有与属性对应集合元素
                // 二是 集合元素值无法转换成目标属性的数据类型

                if (null == nvc[pis[i].Name])
                    continue;

                // 9 个整型,但是Int又有16,32,64位的,这个怎么处理?????
                if (typeof(sbyte) == pis[i].PropertyType)
                {
                    sbyte r; sbyte.TryParse(nvc[pis[i].Name],out r);
                    pis[i].SetValue( obj,r,null );
                }
                else if (typeof(byte) == pis[i].PropertyType)
                {
                    byte r; byte.TryParse(nvc[pis[i].Name], out r);
                    pis[i].SetValue( obj,r,null );
                }
                else if (typeof(char) == pis[i].PropertyType)
                {
                    char r; char.TryParse(nvc[pis[i].Name],out r);
                    pis[i].SetValue( obj,r,null );
                }
                else if (typeof(short) == pis[i].PropertyType)
                {
                    short r; short.TryParse(nvc[pis[i].Name],out r);
                    pis[i].SetValue( obj,r,null );
                }
                else if (typeof(ushort) == pis[i].PropertyType)
                {
                    ushort r; ushort.TryParse(nvc[pis[i].Name], out r);
                    pis[i].SetValue( obj,r,null );
                }
                else if (typeof(int) == pis[i].PropertyType)
                {
                    int r; int.TryParse(nvc[pis[i].Name],out r);
                    pis[i].SetValue( obj,r,null );
                }
                else if (typeof(uint) == pis[i].PropertyType)
                {
                    uint r; uint.TryParse(nvc[pis[i].Name], out r);
                    pis[i].SetValue( obj,r,null );
                }
                else if (typeof(long) == pis[i].PropertyType)
                {
                    long r; long.TryParse(nvc[pis[i].Name], out r);
                    pis[i].SetValue( obj,r,null );
                }
                else if (typeof(ulong) == pis[i].PropertyType)
                {
                    ulong r; ulong.TryParse(nvc[pis[i].Name], out r);
                    pis[i].SetValue( obj,r,null );
                }

                // 2 个浮点型
                else if (typeof(float) == pis[i].PropertyType)
                {
                    float r; float.TryParse(nvc[pis[i].Name], out r);
                    pis[i].SetValue( obj,r,null );
                }
                else if (typeof(double) == pis[i].PropertyType)
                {
                    double r; double.TryParse(nvc[pis[i].Name], out r);
                    pis[i].SetValue( obj,r,null );
                }

                // 1 个数字型-Decimal
                else if (typeof(decimal) == pis[i].PropertyType)
                {
                    decimal r; decimal.TryParse(nvc[pis[i].Name], out r);
                    pis[i].SetValue( obj,r,null );
                }

                // 1 个布尔型
                else if (typeof(bool) == pis[i].PropertyType)
                {
                    bool r; bool.TryParse(nvc[pis[i].Name], out r);
                    pis[i].SetValue( obj,r,null );
                }

                // 1 个结构型--DateTime
                else if (typeof(DateTime) == pis[i].PropertyType)
                {
                    DateTime r; DateTime.TryParse(nvc[pis[i].Name], out r);
                    pis[i].SetValue(obj,r , null);
                }
                
                // 一个字符串类型
                else if (typeof(string) == pis[i].PropertyType)
                {
                    pis[i].SetValue(obj, nvc[pis[i].Name], null);
                }
                
                // 其余为不支持类型
                else
                {
                    //pis[i].SetValue( (nvc[pis[i].Name]);
                    throw new Exception( "CollectionToObject方法不支持此类型转换。" );
                }
            }

            return obj;
        }
        #endregion

抱歉!评论已关闭.