Esempio n. 1
0
        private void VisitedSerialize(
            SerializeData Data,
            object obj,
            SerializeInfo serializer)
        {
            if (serializer.ConstantSize > -1)
            {
                serializer.Serializer(Data, obj);
                return;
            }
            var BytesData = Data.Data;

            if (obj == null)
            {
                BytesData.Write(Byte_Int_N_2, 0, 4);
                return;
            }
            if (serializer.CanStoreInVisit == false)
            {
                BytesData.Write(Byte_Int_N_1, 0, 4);
                serializer.Serializer(Data, obj);
                return;
            }
            var Key = new ObjectContainer()
            {
                HashCode = obj.GetHashCode(),
                obj      = obj
            };
            var Visitor = Data.Visitor;

            if (Visitor.TryGetValue(Key, out var VisitedObj) == false)
            {
                Visitor.Add(Key);
#if DEBUG
                Key.obj = obj;
#endif
                Key.FromPos = (int)BytesData.Position;
                BytesData.Write(Byte_Int_N_1, 0, 4);
                serializer.Serializer(Data, obj);
            }
            else
            {
#if DEBUG
                if (VisitedObj.obj.GetType() != obj.GetType())
                {
                    throw new Exception("Type of visited object is wrong" +
                                        "\n\nMain: " + obj.GetType().ToString() +
                                        "\n\nVisited: " + VisitedObj.obj.GetType().ToString());
                }
#endif
                BytesData.Write(BitConverter.GetBytes(VisitedObj.FromPos), 0, 4);
            }
        }
        private void VisitedSerialize(
            object obj,
            SerializeInfo serializer)
        {
            if (obj == null)
            {
                S_Data.Write(Byte_Int_N_2, 0, 4);
                return;
            }
            if (serializer.CanStoreInVisit == false)
            {
                S_Data.Write(Byte_Int_N_1, 0, 4);
                serializer.Serializer(obj);
                return;
            }
            var VisitedObj = new ObjectContainer()
            {
                ObjHashCode  = obj.GetHashCode(),
                TypeHashCode = serializer.TypeHashCode
            };
            var VisitedPos = Visitor.BinaryInsert(ref VisitedObj);

            if (VisitedPos > -1)
            {
#if DEBUG
                if (VisitedObj.obj.GetType() != obj.GetType())
                {
                    throw new Exception("Type of visited object is wrong" +
                                        "\nMain: " + obj.GetType().ToString() +
                                        "\nVisited: " + VisitedObj.obj.GetType().ToString());
                }
#endif
                S_Data.Write(BitConverter.GetBytes(VisitedObj.FromPos), 0, 4);
                return;
            }
#if DEBUG
            VisitedObj.obj = obj;
#endif
            VisitedObj.FromPos = (int)S_Data.Position;
            S_Data.Write(Byte_Int_N_1, 0, 4);
            serializer.Serializer(obj);
        }