private void setISerializableValues(SerializationInfo info, Type graphType)
        {
            ushort memberCount = this._reader.ReadUInt16();

            for (int i = 0; i < memberCount; i++)
            {
                string memberName  = SerializationStaticHelpers.Read256String(this._reader);
                object memberValue = this.Deserialize(null);
                info.AddValue(memberName, memberValue);
            }
        }
        private void setArrayValues(Array graph, Type graphType)
        {
            Type elementType = graphType.GetElementType();

            for (long i = 0; i < graph.LongLength; i++)
            {
                int[]  multiDimIndex = SerializationStaticHelpers.GetMultiDimAsLongIndex(i, graph);
                object value         = this.Deserialize(elementType);
                graph.SetValue(value, multiDimIndex);
            }
        }
Пример #3
0
        private void getISerializableValues(SerializationInfo info, Type graphType)
        {
            this._writer.Write((ushort)info.MemberCount);
            SerializationInfoEnumerator enumerator = info.GetEnumerator();

            while (enumerator.MoveNext())
            {
                SerializationStaticHelpers.Write256String(enumerator.Name, this._writer);
                this.Serialize(enumerator.Value, null);
            }
        }
Пример #4
0
        private void getObjectValues(object graph, Type graphType)
        {
            bool isAllExcepted = SerializationStaticHelpers.IsAllFieldExpectedType(graphType);

            FieldInfo[]      allFields = graphType.GetFields(SerializationStaticHelpers.SerializationMemberBindings);
            List <FieldInfo> fields    = new List <FieldInfo>();

            for (int i = 0; i < allFields.Length; i++)
            {
                FieldInfo field = allFields[i];
                if (SerializationStaticHelpers.IsSerializable(field))
                {
                    fields.Add(field);
                }
            }

            this._writer.Write((ushort)fields.Count);

            if (isAllExcepted)
            {
                for (int i = 0; i < fields.Count; i++)
                {
                    FieldInfo field = fields[i];
                    SerializationStaticHelpers.Write256String(field.Name, this._writer);
                    object memberValue = field.GetValue(graph);
                    this.Serialize(memberValue, field.FieldType);
                }
            }
            else
            {
                for (int i = 0; i < fields.Count; i++)
                {
                    FieldInfo field = fields[i];
                    SerializationStaticHelpers.Write256String(field.Name, this._writer);
                    object memberValue = field.GetValue(graph);
                    if (SerializationStaticHelpers.GetExpectedFieldTypeAttribute(field) == null)
                    {
                        this.Serialize(memberValue, null);
                    }
                    else
                    {
                        this.Serialize(memberValue, field.FieldType);
                    }
                }
            }
        }
        private void setObjectValues(object graph, Type graphType)
        {
            ushort memberCount = this._reader.ReadUInt16();

            for (int i = 0; i < memberCount; i++)
            {
                string    memberName  = SerializationStaticHelpers.Read256String(this._reader);
                FieldInfo field       = graphType.GetField(memberName, SerializationStaticHelpers.SerializationMemberBindings);
                object    memberValue = null;
                if (field == null)
                {
                    memberValue = this.Deserialize(null);
                    continue;
                }
                memberValue = this.Deserialize(field.FieldType);
                field.SetValue(graph, memberValue);
            }
        }
        public object Deserialize(Type expectedType)
        {
            TypeCode code = (TypeCode)this._reader.ReadByte();

            if (code == TypeCode.Object)
            {
                ushort objId       = this._reader.ReadUInt16();
                object returnGraph = null;

                if (this._readObjects.ContainsKey(objId))
                {
                    returnGraph = this._readObjects[objId];
                }
                else
                {
                    Type   graphType = this.typeReader(expectedType);
                    object graph     = null;

                    ISurrogateSelector      selector  = null;
                    ISerializationSurrogate surrogate = this._selector == null ? null : this._selector.GetSurrogate(graphType, this._context, out selector);
                    this._selector = selector == null ? this._selector : selector;

                    if (graphType.IsArray)
                    {
                        object[] dimensions = new object[graphType.GetArrayRank()];
                        for (int i = 0; i < dimensions.Length; i++)
                        {
                            dimensions[i] = this._reader.ReadInt32();
                        }
                        graph = Activator.CreateInstance(graphType, dimensions);
                        this._readObjects.Add(objId, graph);
                        this.setArrayValues((Array)graph, graphType);
                    }
                    else if (SerializationStaticHelpers.TypeOfISerializable.IsAssignableFrom(graphType))
                    {
                        graph = FormatterServices.GetUninitializedObject(graphType);

                        this._readObjects.Add(objId, graph);

                        ConstructorInfo   serializableConstructor = SerializationStaticHelpers.GetSerializationCtor(graphType);
                        SerializationInfo info = new SerializationInfo(graphType, SerializationStaticHelpers.FormatterConverter);
                        this.setISerializableValues(info, graphType);

                        serializableConstructor.Invoke(graph, new object[] { info, this._context });

                        if (graph is IObjectReference)
                        {
                            object realGraph = ((IObjectReference)graph).GetRealObject(this._context);
                            this._readObjects[objId] = realGraph;
                            graph = realGraph;
                        }

                        if (graph is IDeserializationCallback)
                        {
                            ((IDeserializationCallback)graph).OnDeserialization(graph);
                        }
                    }
                    else if (surrogate == null)
                    {
                        graph = FormatterServices.GetUninitializedObject(graphType);
                        this._readObjects.Add(objId, graph);

                        this.setObjectValues(graph, graphType);

                        if (graph is IDeserializationCallback)
                        {
                            ((IDeserializationCallback)graph).OnDeserialization(graph);
                        }
                    }
                    else
                    {
                        graph = FormatterServices.GetUninitializedObject(graphType);

                        SerializationInfo info = new SerializationInfo(graphType, SerializationStaticHelpers.FormatterConverter);
                        this.setISerializableValues(info, graphType);

                        surrogate.SetObjectData(graph, info, this._context, this._selector);
                        this._readObjects.Add(objId, graph);
                    }
                    returnGraph = graph;
                }

                if (returnGraph is IObjectReference)
                {
                    object realGraph = ((IObjectReference)returnGraph).GetRealObject(this._context);

                    this._readObjects[objId] = realGraph;
                    returnGraph = realGraph;
                }
                return(returnGraph);
            }
            else if (code == TypeCode.String)
            {
                short index = this._reader.ReadInt16();
                if (index == -1)
                {
                    return(this._reader.ReadString());
                }
                else
                {
                    return(this._stringContents[index]);
                }
            }
            else
            {
                if (code == (TypeCode)19)
                {
                    return(this.typeReader(expectedType));
                }
                else
                {
                    return(SerializationStaticHelpers.ReadPrimitive(this._reader, code));
                }
            }
        }
        private Type fullTypeReader()
        {
            TypeState state       = (TypeState)this._reader.ReadByte();
            bool      isArray     = (state & TypeState.Array) == TypeState.Array;
            bool      isPrimitive = (state & TypeState.Primitive) == TypeState.Primitive;
            byte      arrayRank   = isArray ? this._reader.ReadByte() : (byte)0;

            if (isPrimitive)
            {
                TypeCode code      = (TypeCode)this._reader.ReadByte();
                Type     graphType = SerializationStaticHelpers.CodeToType(code);
                if (isArray)
                {
                    graphType = graphType.MakeArrayType(arrayRank);
                }
                return(graphType);
            }

            bool isGeneric = (state & TypeState.Generic) == TypeState.Generic;

            if (isGeneric)
            {
                Type   genericDef       = this.fullTypeReader();
                byte   argCount         = this._reader.ReadByte();
                Type[] genericArguments = new Type[argCount];
                for (int i = 0; i < argCount; i++)
                {
                    genericArguments[i] = this.fullTypeReader();
                }
                Type generic = genericDef.MakeGenericType(genericArguments);
                return(isArray ? generic.MakeArrayType(arrayRank) : generic);
            }

            string boundAssemblyName, boundTypeName;

            byte   asmId  = this._reader.ReadByte();
            ushort typeId = 0;

            if (this._readAssemblies.Count > asmId)
            {
                boundAssemblyName = this._readAssemblies[asmId];
            }
            else
            {
                boundAssemblyName = this._reader.ReadString();
                this._readAssemblies.Add(boundAssemblyName);

                boundTypeName = this._reader.ReadString();
                this._readTypes.Add(boundTypeName);
                if (this._binder == null)
                {
                    Type boundType = Type.GetType(boundTypeName + ", " + boundAssemblyName);
                    return(isArray ? boundType.MakeArrayType(arrayRank) : boundType);
                }

                else
                {
                    Type boundType = this._binder.BindToType(boundAssemblyName, boundTypeName);
                    if (boundType == null)
                    {
                        boundType = Type.GetType(boundTypeName + ", " + boundAssemblyName);
                    }
                    return(isArray ? boundType.MakeArrayType(arrayRank) : boundType);
                }
            }
            typeId = this._reader.ReadUInt16();
            if (this._readTypes.Count > typeId)
            {
                boundTypeName = this._readTypes[typeId];
            }
            else
            {
                boundTypeName = this._reader.ReadString();
                this._readTypes.Add(boundTypeName);
            }

            if (this._binder == null)
            {
                Type boundType = Type.GetType(boundTypeName + ", " + boundAssemblyName);
                return(isArray ? boundType.MakeArrayType(arrayRank) : boundType);
            }
            else
            {
                Type boundType = this._binder.BindToType(boundAssemblyName, boundTypeName);
                if (boundType == null)
                {
                    boundType = Type.GetType(boundTypeName + ", " + boundAssemblyName);
                }
                return(isArray ? boundType.MakeArrayType(arrayRank) : boundType);
            }
        }
Пример #8
0
        internal void Serialize(object graph, Type expectedType)
        {
            Type     graphType = graph == null ? null : graph.GetType();
            TypeCode code      = Type.GetTypeCode(graphType);

            code = code == TypeCode.Object && graphType == SerializationStaticHelpers.RuntimeType ? (TypeCode)19 : code;
            code = graph != null && graph is Enum ? TypeCode.Object : code;

            this._writer.Write((byte)code);
            if (code == TypeCode.Object)
            {
                ushort objId = 1;
                bool   first;
                objId = (ushort)this._objIdGenerator.GetId(graph, out first);
                this._writer.Write(objId);

                if (first)
                {
                    ISurrogateSelector      selector  = null;
                    ISerializationSurrogate surrogate = this._selector == null ? null : this._selector.GetSurrogate(graphType, this._context, out selector);
                    this._selector = selector == null ? this._selector : selector;

                    if (graphType.IsArray)
                    {
                        this.typeWriter(graphType, expectedType);

                        Array arrayGraph = (Array)graph;
                        for (int i = 0; i < arrayGraph.Rank; i++) //Rank değeri Type'ın içinde gidiyor.
                        {
                            this._writer.Write(arrayGraph.GetLength(i));
                        }
                        this.getArrayValues(arrayGraph, graphType);
                    }
                    else if (SerializationStaticHelpers.TypeOfISerializable.IsAssignableFrom(graphType))
                    {
                        SerializationInfo info = new SerializationInfo(graphType, SerializationStaticHelpers.FormatterConverter);
                        ((ISerializable)graph).GetObjectData(info, this._context);
                        Type infoObjType = this._binder == null?Type.GetType(info.FullTypeName + ", " + info.AssemblyName) : this._binder.BindToType(info.AssemblyName, info.FullTypeName);

                        if (infoObjType != graphType)
                        {
                            graphType = infoObjType;
                        }

                        this.typeWriter(graphType, expectedType);
                        this.getISerializableValues(info, graphType);
                    }
                    else if (surrogate == null)
                    {
                        this.typeWriter(graphType, expectedType);
                        this.getObjectValues(graph, graphType);
                    }
                    else
                    {
                        SerializationInfo info = new SerializationInfo(graphType, SerializationStaticHelpers.FormatterConverter);
                        surrogate.GetObjectData(graph, info, this._context);
                        this.typeWriter(graphType, expectedType);
                        this.getISerializableValues(info, graphType);
                    }
                }
            }
            else if (code == TypeCode.String)
            {
                string strGraph = (string)graph;
                short  index    = (short)this._stringContents.IndexOf(strGraph);
                this._writer.Write(index);
                if (index == -1)
                {
                    this._writer.Write(strGraph);
                }
            }
            else
            {
                if (code == (TypeCode)19)
                {
                    this.typeWriter(graph as Type, expectedType);
                }
                else
                {
                    SerializationStaticHelpers.WritePrimitive(this._writer, graph, code);
                }
            }
        }