internal void Init(
     [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type?objectType,
     string[] memberNames,
     Type[]?memberTypes,
     ISurrogateSelector?surrogateSelector,
     StreamingContext context,
     ObjectManager?objectManager,
     SerObjectInfoInit?serObjectInfoInit,
     IFormatterConverter?converter,
     bool bSimpleAssembly)
 {
     _objectType         = objectType;
     _objectManager      = objectManager;
     _wireMemberNames    = memberNames;
     _wireMemberTypes    = memberTypes;
     _context            = context;
     _serObjectInfoInit  = serObjectInfoInit;
     _formatterConverter = converter;
     _isSimpleAssembly   = bSimpleAssembly;
     if (memberTypes != null)
     {
         _isTyped = true;
     }
     if (objectType != null)
     {
         InitReadConstructor(objectType, surrogateSelector, context);
     }
 }
Example #2
0
        internal void Serialize(object graph, BinaryFormatterWriter serWriter)
        {
            if (graph == null)
            {
                throw new ArgumentNullException(nameof(graph));
            }
            if (serWriter == null)
            {
                throw new ArgumentNullException(nameof(serWriter));
            }

            _serWriter = serWriter;

            serWriter.WriteBegin();
            long   headerId = 0;
            object?obj;
            bool   isNew;

            // allocations if methodCall or methodResponse and no graph
            _idGenerator        = new ObjectIDGenerator();
            _objectQueue        = new Queue <object>();
            _formatterConverter = new FormatterConverter();
            _serObjectInfoInit  = new SerObjectInfoInit();

            _topId   = InternalGetId(graph, false, null, out isNew);
            headerId = -1;
            WriteSerializedStreamHeader(_topId, headerId);

            _objectQueue.Enqueue(graph);
            while ((obj = GetNext(out long objectId)) != null)
            {
                WriteObjectInfo?objectInfo = null;

                // GetNext will return either an object or a WriteObjectInfo.
                // A WriteObjectInfo is returned if this object was member of another object
                if (obj is WriteObjectInfo)
                {
                    objectInfo = (WriteObjectInfo)obj;
                }
                else
                {
                    objectInfo          = WriteObjectInfo.Serialize(obj, _surrogates, _context, _serObjectInfoInit, _formatterConverter, this, _binder);
                    objectInfo._assemId = GetAssemblyId(objectInfo);
                }

                objectInfo._objectId = objectId;
                NameInfo typeNameInfo = TypeToNameInfo(objectInfo);
                Write(objectInfo, typeNameInfo, typeNameInfo);
                PutNameInfo(typeNameInfo);
                objectInfo.ObjectEnd();
            }

            serWriter.WriteSerializationHeaderEnd();
            serWriter.WriteEnd();

            // Invoke OnSerialized Event
            _objectManager.RaiseOnSerializedEvent();
        }
        internal object Deserialize(BinaryParser serParser, bool fCheck)
        {
            if (serParser == null)
            {
                throw new ArgumentNullException(nameof(serParser));
            }

            _fullDeserialization = false;
            TopObject            = null;
            _topId = 0;

            _isSimpleAssembly = (_formatterEnums._assemblyFormat == FormatterAssemblyStyle.Simple);

            using (DeserializationToken token = SerializationInfo.StartDeserialization())
            {
                if (_fullDeserialization)
                {
                    // Reinitialize
                    _objectManager     = new ObjectManager(_surrogates, _context);
                    _serObjectInfoInit = new SerObjectInfoInit();
                }

                // Will call back to ParseObject, ParseHeader for each object found
                serParser.Run();

                if (_fullDeserialization)
                {
                    _objectManager !.DoFixups();
                }

                if (TopObject == null)
                {
                    throw new SerializationException(SR.Serialization_TopObject);
                }

                //if TopObject has a surrogate then the actual object may be changed during special fixup
                //So refresh it using topID.
                if (HasSurrogate(TopObject.GetType()) && _topId != 0)//Not yet resolved
                {
                    Debug.Assert(_objectManager != null);
                    TopObject = _objectManager.GetObject(_topId);
                }

                if (TopObject is IObjectReference)
                {
                    TopObject = ((IObjectReference)TopObject).GetRealObject(_context);
                }

                if (_fullDeserialization)
                {
                    _objectManager !.RaiseDeserializationEvent(); // This will raise both IDeserialization and [OnDeserialized] events
                }

                return(TopObject !);
            }
        }
Example #4
0
        internal void Init(Type objectType, ISurrogateSelector?surrogateSelector, StreamingContext context, ObjectManager?objectManager, SerObjectInfoInit?serObjectInfoInit, IFormatterConverter?converter, bool bSimpleAssembly)
        {
            _objectType         = objectType;
            _objectManager      = objectManager;
            _context            = context;
            _serObjectInfoInit  = serObjectInfoInit;
            _formatterConverter = converter;
            _isSimpleAssembly   = bSimpleAssembly;

            InitReadConstructor(objectType, surrogateSelector, context);
        }
        internal static ReadObjectInfo Create(
            [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type objectType,
            ISurrogateSelector?surrogateSelector,
            StreamingContext context,
            ObjectManager?objectManager,
            SerObjectInfoInit?serObjectInfoInit,
            IFormatterConverter?converter,
            bool bSimpleAssembly)
        {
            ReadObjectInfo roi = GetObjectInfo(serObjectInfoInit);

            roi.Init(objectType, surrogateSelector, context, objectManager, serObjectInfoInit, converter, bSimpleAssembly);
            return(roi);
        }
        // Write Constructor used for array types or null members
        internal void InitSerialize(
            [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type objectType,
            ISurrogateSelector?surrogateSelector,
            StreamingContext context,
            SerObjectInfoInit serObjectInfoInit,
            IFormatterConverter converter,
            SerializationBinder?binder)
        {
            _objectType        = objectType;
            _context           = context;
            _serObjectInfoInit = serObjectInfoInit;

            if (objectType.IsArray)
            {
                InitNoMembers();
                return;
            }

            InvokeSerializationBinder(binder);

            if (surrogateSelector != null)
            {
                _serializationSurrogate = surrogateSelector.GetSurrogate(objectType, context, out ISurrogateSelector surrogateSelectorTemp);
            }

            if (_serializationSurrogate != null)
            {
                // surrogate does not have this problem since user has pass in through the BF's ctor
                _si    = new SerializationInfo(objectType, converter);
                _cache = new SerObjectInfoCache(objectType);
                _isSi  = true;
            }
            else if (!ReferenceEquals(objectType, Converter.s_typeofObject) && Converter.s_typeofISerializable.IsAssignableFrom(objectType))
            {
                _si    = new SerializationInfo(objectType, converter);
                _cache = new SerObjectInfoCache(objectType);
                CheckTypeForwardedFrom(_cache, objectType, _binderAssemblyString);
                _isSi = true;
            }

            if (!_isSi)
            {
                InitMemberInfo();
                CheckTypeForwardedFrom(_cache, objectType, _binderAssemblyString);
            }
        }
Example #7
0
        // Write constructor
        internal void InitSerialize(object obj, ISurrogateSelector?surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder?binder)
        {
            _context           = context;
            _obj               = obj;
            _serObjectInfoInit = serObjectInfoInit;
            _objectType        = obj.GetType();

            if (_objectType.IsArray)
            {
                _isArray = true;
                InitNoMembers();
                return;
            }

            InvokeSerializationBinder(binder);
            objectWriter.ObjectManager.RegisterObject(obj);

            ISurrogateSelector surrogateSelectorTemp;

            if (surrogateSelector != null && (_serializationSurrogate = surrogateSelector.GetSurrogate(_objectType, context, out surrogateSelectorTemp)) != null)
            {
                _si = new SerializationInfo(_objectType, converter);
                if (!_objectType.IsPrimitive)
                {
                    _serializationSurrogate.GetObjectData(obj, _si, context);
                }
                InitSiWrite();
            }
            else if (obj is ISerializable)
            {
                if (!_objectType.IsSerializable)
                {
                    throw new SerializationException(SR.Format(SR.Serialization_NonSerType, _objectType.FullName, _objectType.Assembly.FullName));
                }
                _si = new SerializationInfo(_objectType, converter);
                ((ISerializable)obj).GetObjectData(_si, context);
                InitSiWrite();
                CheckTypeForwardedFrom(_cache, _objectType, _binderAssemblyString);
            }
            else
            {
                InitMemberInfo();
                CheckTypeForwardedFrom(_cache, _objectType, _binderAssemblyString);
            }
        }
Example #8
0
        internal static ReadObjectInfo Create(Type?objectType, string[] memberNames, Type[]?memberTypes, ISurrogateSelector?surrogateSelector, StreamingContext context, ObjectManager?objectManager, SerObjectInfoInit?serObjectInfoInit, IFormatterConverter?converter, bool bSimpleAssembly)
        {
            ReadObjectInfo roi = GetObjectInfo(serObjectInfoInit);

            roi.Init(objectType, memberNames, memberTypes, surrogateSelector, context, objectManager, serObjectInfoInit, converter, bSimpleAssembly);
            return(roi);
        }