Ejemplo n.º 1
0
        /// <summary>
        /// 在当前上下文中写入已知类型数据,并移动上下文写入游标索引。
        /// </summary>
        /// <param name="context">当前字节序列上下文</param>
        /// <param name="writer">二进制写入器</param>
        /// <param name="dType">规范数据定义</param>
        /// <param name="objData">写入对象实例</param>
        /// <param name="isNetworkBytes">是否写入网络序</param>
        public static void WriteKnownTypeObject(StreamContext context, BinaryWriter writer, SpecDataType dType, object objData, bool isNetworkBytes)
        {
            Type clrType = dType.GetRuntimeType();

            if (dType.Prototype == SpecDataPrototype.Native)
            {
                context.IncreasePosition(SpecData.CLRObjectWrite(clrType, writer, objData, isNetworkBytes));
            }
            else
            {
                if (dType.Prototype == SpecDataPrototype.Array)
                {
                    Type  elementType = dType.GetElementType();
                    Array arrObj      = (Array)objData;
                    if (SpecDataType.IsNativeType(TypeCache.ToSimpleType(elementType)))
                    {
                        #region 写入基础数据类型数组
                        for (int i = 0, j = dType.ElementLength; i < j; i++)
                        {
                            context.IncreasePosition(SpecData.CLRObjectWrite(elementType, writer, arrObj.GetValue(i), isNetworkBytes));
                        }
                        #endregion
                    }
                    else
                    {
                        for (int m = 0, n = dType.ElementLength; m < n; m++)
                        {
                            WriteKnownTypeObject(context, writer, dType.GetElementDataType(), arrObj.GetValue(m), isNetworkBytes);
                        }
                    }
                }
                else
                {
                    #region 扩展写入
                    if (clrType.Equals(typeof(EnumContract)))
                    {
                        EnumContract enc = (EnumContract)dType.GetDefineInstance();
                        clrType = enc.GetBaseRuntimeType();
                        if (!objData.GetType().Equals(clrType))
                        {
                            objData = enc.GetEnumUnderlyingValue(objData.ToString());
                        }
                        context.IncreasePosition(SpecData.CLRObjectWrite(clrType, writer, objData, isNetworkBytes));
                    }
                    else if (clrType.Equals(typeof(DataContract)))
                    {
                        DataContract dac = (DataContract)dType.GetDefineInstance();
                        WriteByContract(context, writer, (Dictionary <string, object>)objData, dac);
                    }
                    else
                    {
                        throw new SpecDataDefineException(string.Format("不能读取数据类型{0}!", clrType.FullName));
                    }
                    #endregion
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 在当前上下文中读取已知类型读取数据,并移动上下文读取游标索引。
        /// </summary>
        /// <param name="context">当前字节序列上下文</param>
        /// <param name="reader">二进制读取器</param>
        /// <param name="dType">规范数据定义</param>
        /// <returns></returns>
        public static object ReadKnownTypeObject(StreamContext context, BinaryReader reader, SpecDataType dType)
        {
            object itemObj = null;

            Type clrType = dType.GetRuntimeType();

            if (dType.Prototype == SpecDataPrototype.Native)
            {
                itemObj = SpecData.CLRObjectFromReader(clrType, reader);
                context.IncreasePosition(SpecData.GetCLRTypeByteLength(clrType));
            }
            else
            {
                if (dType.Prototype == SpecDataPrototype.Array)
                {
                    Type  elementType = dType.GetElementType();
                    Array arrObj      = Array.CreateInstance(elementType, dType.ElementLength);
                    if (SpecDataType.IsNativeType(TypeCache.ToSimpleType(elementType)))
                    {
                        #region 读取基础数据类型数组
                        long unitLen = SpecData.GetCLRTypeByteLength(elementType);
                        for (int i = 0, j = dType.ElementLength; i < j; i++)
                        {
                            arrObj.SetValue(SpecData.CLRObjectFromReader(elementType, reader), i);
                            context.IncreasePosition(unitLen);
                        }
                        #endregion
                    }
                    else
                    {
                        for (int m = 0, n = dType.ElementLength; m < n; m++)
                        {
                            arrObj.SetValue(ReadKnownTypeObject(context, reader, dType.GetElementDataType()), m);
                        }
                    }
                    itemObj = arrObj;
                }
                else
                {
                    #region 扩展读取
                    if (clrType.Equals(typeof(EnumContract)))
                    {
                        EnumContract enc = (EnumContract)dType.GetDefineInstance();
                        clrType = enc.GetBaseRuntimeType();
                        itemObj = SpecData.CLRObjectFromReader(clrType, reader);
                        context.IncreasePosition(SpecData.GetCLRTypeByteLength(clrType));
                    }
                    else if (clrType.Equals(typeof(DataContract)))
                    {
                        DataContract dac = (DataContract)dType.GetDefineInstance();
                        itemObj = ReadByContract(context, reader, dac);
                    }
                    else
                    {
                        throw new SpecDataDefineException(string.Format("不能读取数据类型{0}!", clrType.FullName));
                    }
                    #endregion
                }
            }

            return(itemObj);
        }