Esempio n. 1
0
        //序列化
        private static void Serialize2(ArrayHelper helper, Type type, object objCh, Encoding encoding)
        {
            //列表中存放基本数据类型
            if (type.IsPrimitive ||
                type.Equals(typeof(string)) ||
                type.Equals(typeof(byte[])))
            {
                SetFieldValue(helper, type, objCh, encoding);
            }

            //列表序列化List<T>,Dictionary<T,T1>,列表中再嵌入列表
            else if (type.Name.Equals(typeof(List <>).Name) ||
                     type.Name.Equals(typeof(ObservableRangeCollection <>).Name) ||
                     type.Name.Equals(typeof(Dictionary <,>).Name))
            {
                Serialize(helper, type, objCh);
            }

            //列表中存放引用类型
            else
            {
                NetObjectAttribute objNameInfo = null;
                List <NetObjectPropertyAttribute> lstFileds = null;
                GetAttributes <object>(objCh, type, true, ref objNameInfo, ref lstFileds);
                Serialize(helper, lstFileds);
            }
        }
Esempio n. 2
0
        //序列化属性
        private static void Serialize(ArrayHelper helper, List <NetObjectPropertyAttribute> lstFileds)
        {
            //数据
            foreach (var item in lstFileds)
            {
                //序列化基本数据类型
                if (item.Type.IsPrimitive ||
                    item.Type.Equals(typeof(string)) ||
                    item.Type.Equals(typeof(byte[])))
                {
                    SetFieldValue(helper, item.Type, item.Value, item.CurrentEncoding);
                }

                //列表序列化List<T>,Dictionary<T,T1>
                else if (item.Type.Name.Equals(typeof(List <>).Name) ||
                         item.Type.Name.Equals(typeof(ObservableRangeCollection <>).Name) ||
                         item.Type.Name.Equals(typeof(Dictionary <,>).Name))
                {
                    Serialize(helper, item.Type, item.Value);
                }

                //普通对象序列化
                else
                {
                    NetObjectAttribute objNameInfo2 = null;
                    List <NetObjectPropertyAttribute> lstFileds2 = null;

                    GetAttributes <object>(item.Value, item.Type, true, ref objNameInfo2, ref lstFileds2);
                    Serialize(helper, lstFileds2);
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// 将byte[]反序列化为指定对象(T)
        /// </summary>
        /// <param name="buf">byte[]</param>
        /// <returns>对象</returns>
        public static T Deserialize <T>(byte[] buf) where T : class, new()
        {
            NetObjectAttribute objNameInfo = null;
            List <NetObjectPropertyAttribute> lstFileds = null;

            GetAttributes <T>(default(T), typeof(T), false, ref objNameInfo, ref lstFileds);

            ArrayHelper helper = new ArrayHelper(NetBase.CommonEncoding, buf);

            //头
            int    dds_magic = helper.DequeueInt32();                                      //消息标识
            string objName   = helper.DequeueStringWithoutEndChar(NetBase.CommonEncoding); //对象名称
            int    ver       = helper.DequeueInt32();                                      //对象版本号

            T instance = new T();

            //反序列化操作
            foreach (var item in lstFileds)
            {
                object val = Deserialize(helper, item.Type, item.CurrentEncoding);
                if (item.Obj.GetType().Name.EndsWith(typeof(PropertyInfo).Name))
                {
                    (item.Obj as PropertyInfo).SetValue(instance, val, null);
                }
                else
                {
                    (item.Obj as FieldInfo).SetValue(instance, val);
                }
            }

            return(instance);
        }
Esempio n. 4
0
        /// <summary>
        /// 获取对象的序列化特性数据
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="obj">对象实例,当为空时,只保存特性对应的属性或者字段引用,否则获取对应属性或者字段的值</param>
        /// <param name="isSerialize">使用方式,true:序列化使用,false:反序列化使用</param>
        /// <param name="objNameInfo">对象信息</param>
        /// <param name="lstFileds">对象字段信息(包括属性信息)</param>
        public static void GetAttributes <T>(T obj, Type objType, bool isSerialize, ref NetObjectAttribute objNameInfo, ref List <NetObjectPropertyAttribute> lstFileds)
        {
            //取得对象名称及版本号
            objNameInfo = GetAttribute <T, NetObjectAttribute>(obj);

            lstFileds = new List <NetObjectPropertyAttribute>();

            //取得需要序列化的属性列表
            PropertyInfo[] pis = objType.GetProperties();
            foreach (PropertyInfo pi in pis)
            {
                NetObjectPropertyAttribute soa = GetAttribute <PropertyInfo, NetObjectPropertyAttribute>(pi);
                if (soa == null)
                {
                    continue;
                }

                soa.Type = pi.PropertyType;
                if (isSerialize)
                {
                    soa.Value = pi.GetValue(obj, null);
                }
                else
                {
                    soa.Obj = pi;
                }

                lstFileds.Add(soa);
            }

            //取得需要序列化的字段列表
            FieldInfo[] fis = objType.GetFields();
            foreach (FieldInfo fi in fis)
            {
                NetObjectPropertyAttribute soa = GetAttribute <FieldInfo, NetObjectPropertyAttribute>(fi);
                if (soa == null)
                {
                    continue;
                }

                soa.Type = fi.FieldType;
                if (isSerialize)
                {
                    soa.Value = fi.GetValue(obj);
                }
                else
                {
                    soa.Obj = fi;
                }

                lstFileds.Add(soa);
            }

            //排序
            lstFileds.Sort((oA, oB) => oA.ID.CompareTo(oB.ID));
        }
Esempio n. 5
0
        //反序列化对象
        private static object Deserialize(ArrayHelper helper, Type childType, Encoding encoding)
        {
            object val = default(object);

            //列表、字典
            if (childType.Name.Equals(typeof(List <>).Name) ||
                childType.Name.Equals(typeof(ObservableRangeCollection <>).Name) ||
                childType.Name.Equals(typeof(Dictionary <,>).Name))
            {
                val = Deserialize2(helper, childType);
            }

            //基本数据类型赋值
            else if (childType.IsPrimitive ||
                     childType.Name.Equals(typeof(string).Name) ||
                     childType.Name.Equals(typeof(byte[]).Name))
            {
                val = GetFieldValue(helper, childType, encoding);
            }

            //引用类型赋值
            else
            {
                NetObjectAttribute objNameInfo = null;
                List <NetObjectPropertyAttribute> lstFileds = null;
                val = childType.Assembly.CreateInstance(childType.FullName);
                GetAttributes(val, childType, false, ref objNameInfo, ref lstFileds);
                foreach (var item in lstFileds)
                {
                    object val2 = Deserialize(helper, item.Type, item.CurrentEncoding);
                    if (item.Obj.GetType().Name.EndsWith(typeof(PropertyInfo).Name))
                    {
                        (item.Obj as PropertyInfo).SetValue(val, val2, null);
                    }
                    else
                    {
                        (item.Obj as FieldInfo).SetValue(val, val2);
                    }
                }
            }

            return(val);
        }
Esempio n. 6
0
        /// <summary>
        /// 将指定对象序列化为byte[]
        /// </summary>
        /// <param name="obj">对象</param>
        /// <param name="needHead">是否需要序列化头部信息(消息标识、对象名称、对象版本号等数据)</param>
        /// <returns>byte[]</returns>
        public static byte[] Serialize <T>(T obj)
        {
            NetObjectAttribute objNameInfo = null;
            List <NetObjectPropertyAttribute> lstFileds = null;

            GetAttributes <T>(obj, typeof(T), true, ref objNameInfo, ref lstFileds);

            ArrayHelper helper = new ArrayHelper(NetBase.CommonEncoding);

            //头
            helper.Enqueue(NetBase.DDS_MAGIC);                                      //消息标识
            helper.EnqueueWithoutEndChar(objNameInfo.Name, NetBase.CommonEncoding); //对象名称
            helper.Enqueue(objNameInfo.Version);                                    //对象版本号

            //类属性
            Serialize(helper, lstFileds);

            return(helper.Arrays);
        }
Esempio n. 7
0
 public static bool IsSame(this NetObject source, NetObjectAttribute dest)
 {
     return(source.Name.Equals(dest.Name) && source.Version.Equals(dest.Version));
 }