Example #1
0
        /// <summary>
        /// 将对象或具有给定根的对象图形序列化为所提供的流。
        /// </summary>
        /// <param name="serializationStream">格式化程序在其中放置序列化数据的流。此流可以引用多种后备存储区(如文件、网络、内存等)。</param>
        /// <param name="graph">要序列化的对象或对象图形的根。将自动序列化此根对象的所有子对象。</param>
        public void Serialize(Stream serializationStream, object graph)
        {
            Type InstanceType = graph.GetType();

            ObjectTransferOrderAttribute[] transConfig = new ObjectTransferOrderAttribute[0];
            PropertyInfo[] transPropertys = SpecUtil.GetTransferProperties(InstanceType, out transConfig);

            byte[] currentBytes = new byte[0];
            ObjectTransferOrderAttribute currentConfig = null;
            PropertyInfo pInfo = null;

            for (int i = 0, j = transPropertys.Length; i < j; i++)
            {
                pInfo         = transPropertys[i];
                currentConfig = transConfig[i];
                currentBytes  = SpecUtil.GetGetHostBytes(pInfo.PropertyType, pInfo.GetValue(graph, null));

                if (currentBytes != null && currentBytes.Length > 0)
                {
                    if (currentConfig.Reverse)
                    {
                        currentBytes = SpecUtil.ReverseBytes(currentBytes);
                    }
                    serializationStream.Write(currentBytes, 0, currentBytes.Length);
                }
            }
        }
Example #2
0
        /// <summary>
        /// 读取全部数据绑定
        /// </summary>
        /// <typeparam name="TEntity">The type of the entity.</typeparam>
        /// <returns></returns>
        public TEntity ReadToEnd <TEntity>()
            where TEntity : ESPDataBase, new()
        {
            TEntity instance     = new TEntity();
            Type    instanceType = typeof(TEntity);

            ObjectTransferOrderAttribute[] Configs = new ObjectTransferOrderAttribute[0];
            PropertyInfo[] bindProperties = SpecUtil.GetTransferProperties(instanceType, out Configs);
            object         currentObj = null, lastDat = null;

            for (int m = 0, n = bindProperties.Length; m < n; m++)
            {
                string propKey = BindBuilder.GetPropertyBindKey(instanceType, bindProperties[m]);
                if (BindBuilder.CustomBindDict.ContainsKey(propKey))
                {
                    currentObj = BindBuilder.CustomBindDict[propKey](bindSteam, instance);
                    //Console.WriteLine("自定义属性绑定回调..");
                }
                else
                {
                    if (lastDat != null && bindProperties[m].PropertyType.IsArray)
                    {
                        if (Configs[m].ArrayLengthOffset != -1)
                        {
                            throw new NotSupportedException(String.Format("数组属性偏移量不为-1的值暂不支持,当前配置值为:{0}!\r\n实例属性:{1}:{2}, 属性类型:{3}",
                                                                          Configs[m].ArrayLengthOffset,
                                                                          instanceType.FullName,
                                                                          bindProperties[m].Name,
                                                                          bindProperties[m].PropertyType.FullName));
                        }
                        else
                        {
                            currentObj = SpecUtil.GetArrayPropertyValue(bindProperties[m].PropertyType.GetElementType(),
                                                                        Convert.ToInt32(lastDat), bindSteam);
                        }
                    }
                    else
                    {
                        currentObj = SpecUtil.GetNetworkStreamMappingData(bindProperties[m].PropertyType, instance.Context, bindSteam, -1, false);
                    }
                }

                lastDat = currentObj;
                bindProperties[m].SetValue(instance, currentObj, null);
            }

            return(instance);
        }
Example #3
0
        /// <summary>
        /// 获取配置成员属性的网络传输字节序列
        /// </summary>
        /// <param name="skipPropertyFn">相关属性是否忽略的判断委托</param>
        /// <returns></returns>
        public byte[] GetInstanceNetworkBytes(Func <PropertyInfo, bool> skipPropertyFn)
        {
            //获取需要传输的字节队列
            //依据传输规则封装
            using (MemoryStream ms = new MemoryStream())
            {
                Type InstanceType = this.GetType();

                ObjectTransferOrderAttribute[] transConfig = new ObjectTransferOrderAttribute[0];
                PropertyInfo[] transPropertys = SpecUtil.GetTransferProperties(InstanceType, out transConfig);

                byte[] currentBytes = new byte[0];
                ObjectTransferOrderAttribute currentConfig = null;
                PropertyInfo pInfo = null;
                for (int i = 0, j = transPropertys.Length; i < j; i++)
                {
                    pInfo = transPropertys[i];
                    if (skipPropertyFn != null && skipPropertyFn(pInfo))
                    {
                        continue;
                    }

                    currentConfig = transConfig[i];
                    currentBytes  = SpecUtil.GetGetHostBytes(pInfo.PropertyType, pInfo.GetValue(this, null));
                    if (currentBytes != null && currentBytes.Length > 0)
                    {
                        if (currentConfig.Reverse)
                        {
                            currentBytes = SpecUtil.ReverseBytes(currentBytes);
                        }
                        ms.Write(currentBytes, 0, currentBytes.Length);

                        //Console.WriteLine("【{3}】获取属性[{0}]的传输字节序列,长度: {1},反转序列:{2}, 数据类型:{4}",
                        //    pInfo.Name, currentBytes.Length,
                        //    currentConfig.Reverse,
                        //    this.GetType().FullName,
                        //    pInfo.PropertyType);
                    }
                }
                return(ms.ToArray());
            }
        }
Example #4
0
        public int ReadOne <TEntity>(string propertyName)
            where TEntity : ESPDataBase, new()
        {
            ObjectTransferOrderAttribute[] configs = new ObjectTransferOrderAttribute[0];
            PropertyInfo[] totalProperties = SpecUtil.GetTransferProperties(typeof(TEntity), out configs);
            int            i = 0, j = totalProperties.Length;
            int            targetIndex = 0;

            for (; i < j; i++)
            {
                if (!string.IsNullOrEmpty(propertyName) &&
                    totalProperties[i].Name.Equals(propertyName, StringComparison.OrdinalIgnoreCase))
                {
                    targetIndex = i;
                    break;
                }
            }

            return(j - i - 1);
        }