Array ReadArray(ReadContext context, Type elementType, Int32 n)
        {
            Array arr = Array.CreateInstance(elementType, n);

            for (int i = 0; i < n; i++)
            {
                Object obj = ReadMember(context.Clone(null, elementType, elementType, i.ToString()) as ReadContext);
                arr.SetValue(obj, i);

                OnDeserialized(context);
            }

            return arr;
        }
        /// <summary>
        /// 从指定流读取数据填充到指定对象
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public Object Deserialize(Stream stream, Object obj)
        {
            ReadContext context = new ReadContext();
            context.Formatter = this;
            context.Reader = new BinaryReaderX(stream);

            // 树,用于记录分析所到达的位置
            context.Node = new ProtocolTreeNode(null, null);
            context.Node.Context = context;

            Type type = null;
            if (obj != null) type = obj.GetType();

            //context.Config = FormatterConfig.Default;
            context.Config = Head.Config;

            // 使用默认设置读取头部
            if (!Head.Config.NoHead)
            {
                ReadMember(context.Clone(Head, Head.GetType(), null, "Head") as ReadContext);
                OnDeserialized(context);

                //// 只有使用了头部,才使用头部设置信息,否则使用默认设置信息,因为远端需要正确识别数据
                //context.Config = Head.Config;
            }

            if (type == null)
            {
                // 读取类名
                Assembly asm = Assembly.Load(Head.AssemblyName);
                if (asm == null) throw new Exception("无法找到程序集" + Head.AssemblyName + "!");

                type = asm.GetType(Head.TypeName);
                if (type == null) throw new Exception("无法找到类" + Head.TypeName + "!");
            }

            context.Type = type;
            context.Node.Type = type;

            if (obj == null) obj = Activator.CreateInstance(type);
            context.Data = obj;

            ReadMember(context.Clone(obj, type, null, "Body") as ReadContext);
            OnDeserialized(context);

            return obj;
        }
        Object ReadObjectRef(ReadContext context)
        {
            if (!context.Config.UseRefObject)
            {
                // 不使用引用对象的时候,这里就需要判断了
                if (context.Node.Depth > 1 && !context.Config.NotNull)
                {
                    // 读取一个字节,探测是否为空
                    Byte b = context.Reader.ReadByte();
                    if (b == 0)
                    {
                        //context.Data = null;
                        return null;
                    }
                }
            }

            // 创建
            if (context.Data == null)
            {
                context.Data = OnCreateInstance(context, context.Type);
                if (context.Data == null) context.Data = Activator.CreateInstance(context.Type);
            }

            // 添加对象到对象集合
            if (context.Config.UseRefObject) context.Objects.Add(context.Data);

            // 先获取类特性,后获取成员特性,所以,成员特性优先于类特性
            context.Config = context.Config.CloneAndMerge(context.Type);

            MemberInfo[] mis = FindAllSerialized(context.Type, context.Config.SerialProperty);
            if (mis != null && mis.Length > 0)
            {
                foreach (MemberInfo item in mis)
                {
                    Object obj = null;
                    if (item is FieldInfo)
                    {
                        FieldInfo fi = item as FieldInfo;
                        FieldInfoX fix = fi;
                        // 只有Object类型才计算值
                        if (Type.GetTypeCode(fi.FieldType) == TypeCode.Object) obj = fix.GetValue(context.Data);
                        obj = ReadMember(context.Clone(obj, fi.FieldType, item) as ReadContext);
                        fix.SetValue(context.Data, obj);

                        OnDeserialized(context);
                    }
                    else
                    {
                        PropertyInfo pi = item as PropertyInfo;
                        PropertyInfoX pix = pi;
                        // 只有Object类型才计算值
                        if (Type.GetTypeCode(pi.PropertyType) == TypeCode.Object) obj = pix.GetValue(context.Data);
                        obj = ReadMember(context.Clone(obj, pi.PropertyType, item) as ReadContext);
                        if (pi.GetSetMethod() != null) pix.SetValue(context.Data, obj);

                        OnDeserialized(context);
                    }
                }
            }
            return context.Data;
        }