/// <summary>
 ///     注册一个智能类型处理器
 ///     <para>* 如果该类型的处理器已经存在,则进行替换操作。</para>
 /// </summary>
 /// <param name="processor">智能类型处理器</param>
 public void Regist(IIntellectTypeProcessor processor)
 {
     if (processor == null)
     {
         return;
     }
     try
     {
         if (_processor.ContainsKey(processor.SupportedType))
         {
             _processor[processor.SupportedType] = processor;
             return;
         }
         _processor.Add(processor.SupportedType, processor);
     }
     catch (System.Exception ex) { _tracing.Error(ex, null); }
 }
Ejemplo n.º 2
0
        /// <summary>
        ///     设置字段实例
        /// </summary>
        /// <param name="instance">对象实例</param>
        /// <param name="analyseResult">字段临时解析结构</param>
        /// <param name="data">元数据</param>
        /// <param name="offset">元数据偏移</param>
        /// <param name="length">元数据可用长度</param>
        public static void SetInstance(object instance, GetObjectAnalyseResult analyseResult, byte[] data, int offset, int length)
        {
            GetObjectAnalyseResult analyze = analyseResult;

            //热处理判断
            if (analyze.HasCacheFinished)
            {
                analyze.CacheProcess(instance, analyseResult, data, offset, length);
                return;
            }

            #region 普通类型判断

            IIntellectTypeProcessor intellectTypeProcessor = IntellectTypeProcessorMapping.Instance.GetProcessor(analyze.Attribute.Id) ??
                                                             IntellectTypeProcessorMapping.Instance.GetProcessor(analyze.Property.PropertyType);
            if (intellectTypeProcessor != null)
            {
                //添加热缓存
                IIntellectTypeProcessor processor = intellectTypeProcessor;
                if (intellectTypeProcessor.SupportUnmanagement)
                {
                    analyze.CacheProcess = processor.Process;
                }
                else
                {
                    analyze.CacheProcess = delegate(object innerInstance, GetObjectAnalyseResult innerAnalyseResult, byte[] parameter, int position, int len) { processor.Process(analyze.Attribute, parameter); }
                };
                analyze.CacheProcess(instance, analyseResult, data, offset, length);
                analyze.HasCacheFinished = true;
                return;
            }

            #endregion

            #region 枚举类型判断

            //枚举类型
            if (analyze.Property.PropertyType.GetTypeInfo().IsEnum)
            {
                Type enumType = Enum.GetUnderlyingType(analyze.Property.PropertyType);
                intellectTypeProcessor = IntellectTypeProcessorMapping.Instance.GetProcessor(enumType);
                if (intellectTypeProcessor == null)
                {
                    throw new System.Exception("Cannot support this enum type! #type: " + analyze.Property.PropertyType);
                }
                //添加热处理
                IIntellectTypeProcessor processor = intellectTypeProcessor;
                if (intellectTypeProcessor.SupportUnmanagement)
                {
                    analyze.CacheProcess = processor.Process;
                }
                else
                {
                    analyze.CacheProcess = delegate(object innerInstance, GetObjectAnalyseResult innerAnalyseResult, byte[] parameter, int position, int len) { processor.Process(analyze.Attribute, parameter); }
                };
                analyze.CacheProcess(instance, analyseResult, data, offset, length);
                analyze.HasCacheFinished = true;
                return;
            }

            #endregion

            #region 可空类型判断

            Type innerType;
            if ((innerType = Nullable.GetUnderlyingType(analyze.Property.PropertyType)) != null)
            {
                intellectTypeProcessor = IntellectTypeProcessorMapping.Instance.GetProcessor(innerType);
                if (intellectTypeProcessor != null)
                {
                    //添加热缓存
                    IIntellectTypeProcessor processor = intellectTypeProcessor;
                    if (intellectTypeProcessor.SupportUnmanagement)
                    {
                        analyze.CacheProcess = processor.Process;
                    }
                    else
                    {
                        analyze.CacheProcess = delegate(object innerInstance, GetObjectAnalyseResult innerAnalyseResult, byte[] parameter, int position, int len) { processor.Process(analyze.Attribute, parameter); }
                    };
                    analyze.CacheProcess(instance, analyseResult, data, offset, length);
                    analyze.HasCacheFinished = true;
                    return;
                }
                throw new System.Exception("Cannot find compatible processor, #type: " + analyze.Property.PropertyType);
            }

            #endregion

            #region 智能类型的判断

            //智能对象的判断
            if (analyze.Property.PropertyType.GetTypeInfo().IsClass&& analyze.Property.PropertyType.GetTypeInfo().GetInterface(Consts.IntellectObjectFullName) != null)
            {
                //添加热缓存
                analyze.CacheProcess = delegate(Object innerInstance, GetObjectAnalyseResult innerAnalyseResult, byte[] parameter, int position, int len)
                {
                    innerAnalyseResult.SetValue(innerInstance, IntellectObjectEngine.GetObject <IntellectObject>(analyze.Property.PropertyType, parameter, position, len));
                };
                analyze.CacheProcess(instance, analyseResult, data, offset, length);
                analyze.HasCacheFinished = true;
                return;
            }

            #endregion

            #region 数组的判断

            if (analyze.Property.PropertyType.IsArray)
            {
                Type elementType = analyze.Property.PropertyType.GetElementType();
                VT   vt          = FixedTypeManager.IsVT(elementType);
                //VT type.
                if (vt != null)
                {
                    #region VT type array processor.

                    IIntellectTypeProcessor arrayProcessor = ArrayTypeProcessorMapping.Instance.GetProcessor(analyseResult.Property.PropertyType);
                    //special optimize.
                    if (arrayProcessor != null)
                    {
                        analyze.CacheProcess = arrayProcessor.Process;
                        analyze.CacheProcess(instance, analyseResult, data, offset, length);
                    }
                    //normally process.
                    else
                    {
                        throw new DefineNoMeaningException(string.Format(ExceptionMessage.EX_VT_FIND_NOT_PROCESSOR, analyze.Attribute.Id, analyze.Property.Name, analyze.Property.PropertyType));
                    }

                    #endregion
                }
                else if (elementType.GetTypeInfo().IsSubclassOf(typeof(IntellectObject)))
                {
                    #region IntellectObject type array processor.

                    //add HOT cache.
                    analyze.CacheProcess = delegate(Object innerInstance, GetObjectAnalyseResult innerAnalyseResult, byte[] parameter, int position, int len)
                    {
                        int innerOffset = position;
                        int chunkSize   = position + len;
                        int arrLen      = BitConverter.ToInt32(parameter, innerOffset);
                        Func <int, IntellectObject[]> func = IntellectObjectArrayHelper.GetFunc <IntellectObject>(analyze.Property.PropertyType);
                        if (arrLen == 0)
                        {
                            innerAnalyseResult.SetValue(innerInstance, func(0));
                            return;
                        }
                        innerOffset += 4;
                        IntellectObject[] array = func(arrLen);
                        int   arrIndex          = 0;
                        short size;
                        do
                        {
                            size         = BitConverter.ToInt16(parameter, innerOffset);
                            innerOffset += 2;
                            if ((parameter.Length - innerOffset) < size)
                            {
                                throw new System.Exception("Illegal remaining binary data length!");
                            }
                            //use unmanagement method by default.
                            if (size == 0)
                            {
                                array[arrIndex] = null;
                            }
                            else
                            {
                                array[arrIndex] = IntellectObjectEngine.GetObject <IntellectObject>(elementType, parameter, innerOffset, size);
                            }
                            innerOffset += size;
                            arrIndex++;
                        } while (innerOffset < parameter.Length && innerOffset < chunkSize);
                        innerAnalyseResult.SetValue(innerInstance, array);
                    };

                    #endregion
                }
                else if (!(elementType == typeof(string)) && elementType.GetTypeInfo().IsSerializable)
                {
                    throw new NotSupportedException(string.Format(ExceptionMessage.EX_NOT_SUPPORTED_VALUE, analyseResult.Attribute.Id, analyseResult.Property.Name, analyseResult.Property.PropertyType));
                }
                else if (elementType == typeof(string))
                {
                    #region Any types if it can get the processor.
                    intellectTypeProcessor = ArrayTypeProcessorMapping.Instance.GetProcessor(analyze.Property.PropertyType);
                    if (intellectTypeProcessor == null)
                    {
                        throw new System.Exception("Cannot support this array element type processor! #type: " + elementType);
                    }
                    //Add hot cache.
                    analyze.CacheProcess = intellectTypeProcessor.Process;
                    #endregion
                }
                else
                {
                    throw new NotSupportedException(string.Format(ExceptionMessage.EX_NOT_SUPPORTED_VALUE, analyseResult.Attribute.Id, analyseResult.Property.Name, analyseResult.Property.PropertyType));
                }
                analyze.HasCacheFinished = true;
                analyze.CacheProcess(instance, analyseResult, data, offset, length);
                return;
            }

            #endregion

            throw new System.Exception("Cannot support this data type: " + analyze.Property.PropertyType);
        }

        #endregion
    }
}
Ejemplo n.º 3
0
        /// <summary>
        ///     将二进制数据反序列化成指定类型对象
        /// </summary>
        /// <param name="type">类型</param>
        /// <param name="data">二进制数据</param>
        /// <returns>返回反序列化后的对象, 如果data为null, 则返回null</returns>
        /// <exception cref="ArgumentNullException">type 参数不能为空</exception>
        public static object GetObject(Type type, byte[] data)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (data == null)
            {
                return(null);
            }
            Type innerType;
            Func <byte[], object>   cache;
            IIntellectTypeProcessor intellectTypeProcessor;

            lock (_deserLockObj)
            {
                if (_deserializers.TryGetValue(type.FullName, out cache))
                {
                    return(cache(data));
                }

                #region 普通类型判断

                intellectTypeProcessor = IntellectTypeProcessorMapping.Instance.GetProcessor(type);
                if (intellectTypeProcessor != null)
                {
                    //添加热缓存
                    IIntellectTypeProcessor processor = intellectTypeProcessor;
                    _deserializers.Add(type.FullName,
                                       cache =
                                           parameter =>
                    {
                        return(processor.Process(IntellectTypeProcessorMapping.DefaultAttribute, parameter));
                    });
                }

                #endregion

                #region 枚举类型判断

                //枚举类型
                else if (type.GetTypeInfo().IsEnum)
                {
                    Type enumType = Enum.GetUnderlyingType(type);
                    intellectTypeProcessor = IntellectTypeProcessorMapping.Instance.GetProcessor(enumType);
                    if (intellectTypeProcessor == null)
                    {
                        throw new NotSupportedException(string.Format(
                                                            ExceptionMessage.EX_NOT_SUPPORTED_VALUE_TEMPORARY, type));
                    }
                    IIntellectTypeProcessor processor = intellectTypeProcessor;
                    _deserializers.Add(type.FullName, cache = parameter => { return(processor.Process(IntellectTypeProcessorMapping.DefaultAttribute, parameter)); });
                }

                #endregion

                #region 可空类型判断

                else if ((innerType = Nullable.GetUnderlyingType(type)) != null)
                {
                    intellectTypeProcessor = IntellectTypeProcessorMapping.Instance.GetProcessor(innerType);
                    if (intellectTypeProcessor == null)
                    {
                        throw new NotSupportedException(string.Format(
                                                            ExceptionMessage.EX_NOT_SUPPORTED_VALUE_TEMPORARY, type));
                    }
                    IIntellectTypeProcessor processor = intellectTypeProcessor;
                    _deserializers.Add(type.FullName, cache = parameter => { return(processor.Process(IntellectTypeProcessorMapping.DefaultAttribute, parameter)); });
                }

                #endregion

                #region 智能类型的判断

                //智能对象的判断
                else if (type.GetTypeInfo().IsSubclassOf(typeof(IntellectObject)))
                {
                    _deserializers.Add(type.FullName, cache = parameter => { return(IntellectObjectEngine.GetObject <object>(type, parameter)); });
                }

                #endregion

                #region 数组的判断

                else if (type.IsArray)
                {
                    Type elementType = type.GetElementType();
                    IIntellectTypeProcessor arrayProcessor = ArrayTypeProcessorMapping.Instance.GetProcessor(type);
                    //VT type.
                    if (arrayProcessor != null)
                    {
                        _deserializers.Add(type.FullName, cache = parameter => { return(arrayProcessor.Process(IntellectTypeProcessorMapping.DefaultAttribute, parameter)); });
                    }
                    else if (elementType.GetTypeInfo().IsSubclassOf(typeof(IntellectObject)))
                    {
                        #region IntellectObject type array processor.

                        _deserializers.Add(type.FullName, cache = parameter => { Func <int, IntellectObject[]> func = IntellectObjectArrayHelper.GetFunc <IntellectObject>(type);
                                                                                 int arrLen = BitConverter.ToInt32(parameter, 0);
                                                                                 if (arrLen == 0)
                                                                                 {
                                                                                     return(func(0));
                                                                                 }
                                                                                 IntellectObject[] array = func(arrLen);
                                                                                 int innerOffset         = 4;
                                                                                 ushort size;
                                                                                 for (int i = 0; i < array.Length; i++)
                                                                                 {
                                                                                     size         = BitConverter.ToUInt16(parameter, innerOffset);
                                                                                     innerOffset += 2;
                                                                                     if (size == 0)
                                                                                     {
                                                                                         array[i] = null;
                                                                                     }
                                                                                     else
                                                                                     {
                                                                                         array[i] = IntellectObjectEngine.GetObject <IntellectObject>(elementType, parameter,
                                                                                                                                                      innerOffset, size);
                                                                                     }
                                                                                     innerOffset += size;
                                                                                 }
                                                                                 return(array); });

                        #endregion
                    }
                    else
                    {
                        throw new NotSupportedException(string.Format(
                                                            ExceptionMessage.EX_NOT_SUPPORTED_VALUE_TEMPORARY, type));
                    }
                }

                #endregion

                return(cache(data));
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        ///     将指定类型的实例序列化为二进制数据
        /// </summary>
        /// <param name="type">类型</param>
        /// <param name="instance">实例对象</param>
        /// <param name="proxy">内存段代理器</param>
        /// <returns>返回序列化后的二进制数据, 如果instance为null, 则返回null</returns>
        /// <exception cref="NotSupportedException">不被支持的类型</exception>
        private static void ToBytes(Type type, object instance, IMemorySegmentProxy proxy)
        {
            Type innerType;
            Action <IMemorySegmentProxy, object> cache;

            lock (_serLockObj)
            {
                if (_serializers.TryGetValue(type.FullName, out cache))
                {
                    cache(proxy, instance);
                    return;
                }
                IIntellectTypeProcessor intellectTypeProcessor;

                #region 普通类型判断

                intellectTypeProcessor = IntellectTypeProcessorMapping.Instance.GetProcessor(type);
                if (intellectTypeProcessor != null)
                {
                    //添加热缓存
                    IIntellectTypeProcessor processor = intellectTypeProcessor;
                    _serializers.Add(type.FullName, (cache = (innerProxy, innerObj) => processor.Process(innerProxy, innerObj)));
                }

                #endregion

                #region 枚举类型判断

                //枚举类型
                else if (type.GetTypeInfo().IsEnum)
                {
                    //获取枚举类型
                    Type enumType = Enum.GetUnderlyingType(type);
                    intellectTypeProcessor = IntellectTypeProcessorMapping.Instance.GetProcessor(enumType);
                    if (intellectTypeProcessor == null)
                    {
                        throw new NotSupportedException(string.Format(ExceptionMessage.EX_NOT_SUPPORTED_VALUE_TEMPORARY, type));
                    }
                    IIntellectTypeProcessor processor = intellectTypeProcessor;
                    _serializers.Add(type.FullName, (cache = (innerProxy, innerObj) => processor.Process(innerProxy, innerObj)));
                }

                #endregion

                #region 可空类型判断

                else if ((innerType = Nullable.GetUnderlyingType(type)) != null)
                {
                    intellectTypeProcessor = IntellectTypeProcessorMapping.Instance.GetProcessor(innerType);
                    if (intellectTypeProcessor == null)
                    {
                        throw new NotSupportedException(string.Format(ExceptionMessage.EX_NOT_SUPPORTED_VALUE_TEMPORARY, innerType));
                    }
                    IIntellectTypeProcessor processor = intellectTypeProcessor;
                    _serializers.Add(type.FullName, (cache = (innerProxy, innerObj) => processor.Process(innerProxy, innerObj)));
                }

                #endregion

                #region 智能对象类型判断

                else if (type.GetTypeInfo().IsSubclassOf(typeof(IntellectObject)))
                {
                    _serializers.Add(type.FullName, (cache = (innerProxy, innerObj) => { IntellectObjectEngine.ToBytes((IIntellectObject)innerObj, innerProxy); }));
                }

                #endregion

                #region 数组的判断

                else if (type.IsArray)
                {
                    if (!type.HasElementType)
                    {
                        throw new NotSupportedException(string.Format(ExceptionMessage.EX_NOT_SUPPORTED_VALUE_TEMPORARY, type));
                    }
                    Type elementType = type.GetElementType();
                    VT   vt          = FixedTypeManager.IsVT(elementType);
                    IIntellectTypeProcessor arrayProcessor = ArrayTypeProcessorMapping.Instance.GetProcessor(type);
                    if (arrayProcessor != null)
                    {
                        _serializers.Add(type.FullName, (cache = (innerProxy, innerObj) => arrayProcessor.Process(innerProxy, innerObj)));
                    }
                    else if (vt != null)
                    {
                        throw new NotSupportedException(string.Format(ExceptionMessage.EX_NOT_SUPPORTED_VALUE_TEMPORARY, type));
                    }
                    else if (elementType.GetTypeInfo().IsSubclassOf(typeof(IntellectObject)))
                    {
                        //Add hot cache.
                        _serializers.Add(type.FullName, (cache = (innerProxy, innerObj) =>
                        {
                            IIntellectObject[] array = (IIntellectObject[])innerObj;
                            if (array == null || array.Length == 0)
                            {
                                return;
                            }
                            //write array length.
                            innerProxy.WriteUInt32((uint)array.Length);
                            for (int i = 0; i < array.Length; i++)
                            {
                                if (array[i] == null)
                                {
                                    innerProxy.WriteUInt16(0);
                                }
                                else
                                {
                                    MemoryPosition innerStartObjPosition = innerProxy.GetPosition();
                                    innerProxy.Skip(Size.UInt16);
                                    IntellectObjectEngine.ToBytes(array[i], innerProxy);
                                    MemoryPosition innerEndObjPosition = innerProxy.GetPosition();
                                    innerProxy.WriteBackUInt16(innerStartObjPosition, (ushort)(MemoryPosition.CalcLength(innerProxy.SegmentCount, innerStartObjPosition, innerEndObjPosition) - 2));
                                }
                            }
                        }));
                    }
                    else
                    {
                        throw new NotSupportedException(string.Format(ExceptionMessage.EX_NOT_SUPPORTED_VALUE_TEMPORARY, type));
                    }
                }

                #endregion

                cache(proxy, instance);
            }
        }
        /// <summary>
        ///     将一个智能对象转换为二进制元数据
        /// </summary>
        /// <param name="obj">智能对象</param>
        /// <param name="proxy">内存段代理器</param>
        /// <returns>返回当前已经被序列化对象的总体长度</returns>
        internal static int ToBytes(IIntellectObject obj, IMemorySegmentProxy proxy)
        {
            //获取智能对象中的智能属性,并按照Id来排序
            ToBytesAnalyseResult[] properties = Analyser.ToBytesAnalyser.Analyse(obj);
            if (properties.Length == 0)
            {
                return(-1);
            }
            MemoryPosition wrapperStartPosition = proxy.GetPosition();

            proxy.Skip(4U);
            IIntellectTypeProcessor intellectTypeProcessor;

            for (int l = 0; l < properties.Length; l++)
            {
                ToBytesAnalyseResult property = properties[l];
                //先检查完全缓存机制
                if (property.HasCacheFinished)
                {
                    property.CacheProcess(proxy, property.Attribute, property, obj, false, false);
                    continue;
                }

                #region 普通类型判断

                intellectTypeProcessor = IntellectTypeProcessorMapping.Instance.GetProcessor(property.Attribute.Id) ??
                                         IntellectTypeProcessorMapping.Instance.GetProcessor(property.Property.PropertyType);
                if (intellectTypeProcessor != null)
                {
                    //添加热缓存
                    IIntellectTypeProcessor processor = intellectTypeProcessor;
                    property.CacheProcess = processor.Process;
                    property.CacheProcess(proxy, property.Attribute, property, obj, false, false);
                    property.HasCacheFinished = true;
                    continue;
                }

                #endregion

                #region 枚举类型判断

                //枚举类型
                if (property.Property.PropertyType.GetTypeInfo().IsEnum)
                {
                    //获取枚举类型
                    Type enumType = Enum.GetUnderlyingType(property.Property.PropertyType);
                    intellectTypeProcessor = IntellectTypeProcessorMapping.Instance.GetProcessor(enumType);
                    if (intellectTypeProcessor == null)
                    {
                        throw new NotSupportedException(string.Format(ExceptionMessage.EX_NOT_SUPPORTED_VALUE, property.Attribute.Id, property.Property.Name, property.Property.PropertyType));
                    }
                    //添加热缓存
                    IIntellectTypeProcessor processor = intellectTypeProcessor;
                    property.CacheProcess = processor.Process;
                    property.CacheProcess(proxy, property.Attribute, property, obj, false, false);
                    property.HasCacheFinished = true;
                    continue;
                }

                #endregion

                #region 可空类型判断

                Type innerType;
                if ((innerType = Nullable.GetUnderlyingType(property.Property.PropertyType)) != null)
                {
                    intellectTypeProcessor = IntellectTypeProcessorMapping.Instance.GetProcessor(innerType);
                    if (intellectTypeProcessor != null)
                    {
                        //添加热缓存
                        IIntellectTypeProcessor processor = intellectTypeProcessor;
                        property.CacheProcess = delegate(IMemorySegmentProxy innerProxy, IntellectPropertyAttribute innerAttribute, ToBytesAnalyseResult innerAnalyseResult, object innerTarget, bool innerIsArrayElement, bool innerNullable)
                        {
                            processor.Process(innerProxy, innerAttribute, innerAnalyseResult, innerTarget, innerIsArrayElement, true);
                        };
                        property.CacheProcess(proxy, property.Attribute, property, obj, false, true);
                        property.HasCacheFinished = true;
                        continue;
                    }
                    throw new NotSupportedException(string.Format(ExceptionMessage.EX_NOT_SUPPORTED_VALUE, property.Attribute.Id, property.Property.Name, property.Property.PropertyType));
                }

                #endregion

                #region 智能对象类型判断

                if (property.Property.PropertyType.GetTypeInfo().GetInterface(Consts.IntellectObjectFullName) != null)
                {
                    //添加热缓存
                    property.CacheProcess = delegate(IMemorySegmentProxy innerProxy, IntellectPropertyAttribute innerAttribute, ToBytesAnalyseResult innerAnalyseResult, object innerTarget, bool innerIsArrayElement, bool innerNullable)
                    {
                        IntellectObject innerIntellectObj = innerAnalyseResult.GetValue <IntellectObject>(innerTarget);
                        if (innerIntellectObj == null)
                        {
                            return;
                        }
                        innerProxy.WriteByte((byte)innerAttribute.Id);
                        MemoryPosition startPos = innerProxy.GetPosition();
                        innerProxy.Skip(4);
                        ToBytes(innerIntellectObj, innerProxy);
                        MemoryPosition endPos = innerProxy.GetPosition();
                        innerProxy.WriteBackInt32(startPos, MemoryPosition.CalcLength(innerProxy.SegmentCount, startPos, endPos) - 4);
                    };
                    property.CacheProcess(proxy, property.Attribute, property, obj, false, false);
                    property.HasCacheFinished = true;
                    continue;
                }

                #endregion

                #region 数组的判断

                if (property.Property.PropertyType.IsArray)
                {
                    if (!property.Property.PropertyType.HasElementType)
                    {
                        throw new NotSupportedException(string.Format(ExceptionMessage.EX_NOT_SUPPORTED_VALUE, property.Attribute.Id, property.Property.Name, property.Property.PropertyType));
                    }
                    Type elementType = property.Property.PropertyType.GetElementType();
                    VT   vt          = FixedTypeManager.IsVT(elementType);
                    //special optimize.
                    IIntellectTypeProcessor arrayProcessor = ArrayTypeProcessorMapping.Instance.GetProcessor(property.Property.PropertyType);
                    if (arrayProcessor != null)
                    {
                        property.CacheProcess = arrayProcessor.Process;
                    }
                    //is VT, but cannot find special processor.
                    else if (vt != null)
                    {
                        throw new NotSupportedException(string.Format(ExceptionMessage.EX_NOT_SUPPORTED_VALUE, property.Attribute.Id, property.Property.Name, property.Property.PropertyType));
                    }
                    else if (elementType.GetTypeInfo().IsSubclassOf(typeof(IntellectObject)))
                    {
                        //Add hot cache.
                        property.CacheProcess = delegate(IMemorySegmentProxy innerProxy, IntellectPropertyAttribute innerAttribute, ToBytesAnalyseResult innerAnalyseResult, object innerTarget, bool innerIsArrayElement, bool innerNullable)
                        {
                            IIntellectObject[] array = innerAnalyseResult.GetValue <IIntellectObject[]>(innerTarget);
                            if (array == null)
                            {
                                if (!innerAttribute.IsRequire)
                                {
                                    return;
                                }
                                throw new PropertyNullValueException(string.Format(ExceptionMessage.EX_PROPERTY_VALUE, innerAttribute.Id, innerAnalyseResult.Property.Name, innerAnalyseResult.Property.PropertyType));
                            }
                            //id(1) + total length(4) + rank(4)
                            innerProxy.WriteByte((byte)innerAttribute.Id);
                            MemoryPosition startPosition = innerProxy.GetPosition();
                            innerProxy.Skip(4U);
                            innerProxy.WriteInt32(array.Length);
                            for (int i = 0; i < array.Length; i++)
                            {
                                IIntellectObject element = array[i];
                                if (element == null)
                                {
                                    innerProxy.WriteUInt16(0);
                                }
                                else
                                {
                                    MemoryPosition innerStartObjPosition = innerProxy.GetPosition();
                                    innerProxy.Skip(Size.UInt16);
                                    ToBytes(element, innerProxy);
                                    MemoryPosition innerEndObjPosition = innerProxy.GetPosition();
                                    innerProxy.WriteBackUInt16(innerStartObjPosition, (ushort)(MemoryPosition.CalcLength(innerProxy.SegmentCount, innerStartObjPosition, innerEndObjPosition) - 2));
                                }
                            }
                            MemoryPosition endPosition = innerProxy.GetPosition();
                            innerProxy.WriteBackInt32(startPosition, MemoryPosition.CalcLength(innerProxy.SegmentCount, startPosition, endPosition) - 4);
                        };
                    }
                    else if (!(elementType == typeof(string)) && elementType.GetTypeInfo().IsSerializable)
                    {
                        throw new NotSupportedException(string.Format(ExceptionMessage.EX_NOT_SUPPORTED_VALUE, property.Attribute.Id, property.Property.Name, property.Property.PropertyType));
                    }
                    else
                    {
                        intellectTypeProcessor = IntellectTypeProcessorMapping.Instance.GetProcessor(property.Attribute.Id) ??
                                                 IntellectTypeProcessorMapping.Instance.GetProcessor(elementType);
                        if (intellectTypeProcessor == null)
                        {
                            throw new NotSupportedException(string.Format(ExceptionMessage.EX_NOT_SUPPORTED_VALUE, property.Attribute.Id, property.Property.Name, elementType));
                        }
                        //Add hot cache.
                        IIntellectTypeProcessor processor = intellectTypeProcessor;
                        property.CacheProcess = delegate(IMemorySegmentProxy innerProxy, IntellectPropertyAttribute innerAttribute, ToBytesAnalyseResult innerAnalyseResult, object innerTarget, bool innerIsArrayElement, bool innerNullable)
                        {
                            Array array = innerAnalyseResult.GetValue <Array>(innerTarget);
                            if (array == null)
                            {
                                if (!innerAttribute.IsRequire)
                                {
                                    return;
                                }
                                throw new PropertyNullValueException(string.Format(ExceptionMessage.EX_PROPERTY_VALUE, innerAttribute.Id, innerAnalyseResult.Property.Name, innerAnalyseResult.Property.PropertyType));
                            }
                            //id(1) + total length(4) + rank(4)
                            innerProxy.WriteByte((byte)innerAttribute.Id);
                            MemoryPosition startPosition = innerProxy.GetPosition();
                            innerProxy.Skip(4);
                            innerProxy.WriteInt32(array.Length);
                            for (int i = 0; i < array.Length; i++)
                            {
                                object element = array.GetValue(i);
                                if (element == null)
                                {
                                    innerProxy.WriteUInt16(0);
                                }
                                else
                                {
                                    MemoryPosition innerStartObjPosition = innerProxy.GetPosition();
                                    innerProxy.Skip(Size.UInt16);
                                    processor.Process(innerProxy, innerAttribute, innerAnalyseResult, element, true);
                                    MemoryPosition innerEndObjPosition = innerProxy.GetPosition();
                                    innerProxy.WriteBackUInt16(innerStartObjPosition, (ushort)(MemoryPosition.CalcLength(innerProxy.SegmentCount, innerStartObjPosition, innerEndObjPosition) - 2));
                                }
                            }
                            MemoryPosition endPosition = innerProxy.GetPosition();
                            innerProxy.WriteBackInt32(startPosition, MemoryPosition.CalcLength(innerProxy.SegmentCount, startPosition, endPosition) - 4);
                        };
                    }
                    property.CacheProcess(proxy, property.Attribute, property, obj, false, false);
                    property.HasCacheFinished = true;
                    continue;
                }

                #endregion

                throw new NotSupportedException(string.Format(ExceptionMessage.EX_NOT_SUPPORTED_VALUE, property.Attribute.Id, property.Property.Name, property.Property.PropertyType));
            }
            MemoryPosition wrapperEndPosition = proxy.GetPosition();
            int            length             = MemoryPosition.CalcLength(proxy.SegmentCount, wrapperStartPosition, wrapperEndPosition);
            proxy.WriteBackInt32(wrapperStartPosition, length);
            return(length);
        }