public void GetObjectData(object /*!*/ obj, SerializationInfo /*!*/ info, StreamingContext context)
        {
            DObject instance = (DObject)obj;

            if ((context.State & StreamingContextStates.Persistence) != StreamingContextStates.Persistence)
            {
                Serialization.DebugInstanceSerialized(instance, false);

                // serialization is requested by Remoting -> serialize everything and do not change type
                MemberInfo[] members = FormatterServices.GetSerializableMembers(instance.GetType());
                info.AddValue(MembersSerializationInfoKey, FormatterServices.GetObjectData(instance, members));
            }
            else
            {
                Serialization.DebugInstanceSerialized(instance, true);

                // Serialization was requested by the user via the serialize() PHP function so it is possible that
                // the type of this instance will be undefined at deserialization time.

                if (instance.RealObject is Library.SPL.Serializable)
                {
                    // the instance is PHP5.1 serializable -> reroute the deserialization to SPLDeserializer
                    SPLDeserializer.GetObjectData(instance, info, context);
                }
                else
                {
                    // otherwise reroute the deserialization to Deserializer, which handles __sleep
                    Deserializer.GetObjectData(instance, info, context);
                }
            }
        }
        /// <include file='Doc/Common.xml' path='/docs/method[@name="OnDeserialization"]/*'/>
        public virtual void OnDeserialization(object sender)
        {
            if (instance == null)
            {
                return;
            }

            object real_object = serInfo.GetValue(ClrRealObjectSerializationInfoKey, typeof(object));

            if (real_object != null)
            {
                // if we have serialized CLR real object, populate the instance now
                if (instance is __PHP_Incomplete_Class)
                {
                    Serialization.SetProperty(
                        instance,
                        ClrRealObjectSerializationInfoKey,
                        ClrObject.WrapRealObject(real_object),
                        context.ScriptContext);
                }
                else if (instance is IClrValue)
                {
                    Type      type  = instance.GetType(); // generic type ClrValue<T>
                    FieldInfo field = type.GetField("realValue");
                    Debug.Assert(field != null);
                    field.SetValue(instance, real_object);
                }
                else
                {
                    ((ClrObject)instance).SetRealObject(real_object);
                }
            }

            // deserialize fields
            SerializationInfoEnumerator enumerator = serInfo.GetEnumerator();

            while (enumerator.MoveNext())
            {
                string name = enumerator.Name;

                if (name != __PHP_Incomplete_Class.ClassNameFieldName &&
                    name != ClrRealObjectSerializationInfoKey)
                {
                    Serialization.SetProperty(
                        instance,
                        name,
                        UnwrapPropertyValue(enumerator.Value),
                        context.ScriptContext);
                }
            }

            Serialization.DebugInstanceDeserialized(instance, true);

            instance.Wakeup(context.ClassContext, context.ScriptContext);
        }
        /// <summary>
        /// Runs when the entire object graph has been deserialized.
        /// </summary>
        /// <param name="obj">The object being deserialized.</param>
        public void OnDeserialization(object /*!*/ obj)
        {
            DObject instance = (DObject)obj;

            MemberInfo[] members = FormatterServices.GetSerializableMembers(instance.GetType());

            // get deserialized members from the temp storage
            Debug.Assert(instance.RuntimeFields != null && instance.RuntimeFields.Count == 1);

            object[] deserialized_members = (object[])instance.RuntimeFields[MembersSerializationInfoKey];
            instance.RuntimeFields.Clear();

            if (deserialized_members.Length != members.Length)
            {
                throw new InvalidOperationException();
            }

            FormatterServices.PopulateObjectMembers(instance, members, deserialized_members);

            Serialization.DebugInstanceDeserialized(instance, false);
        }
			/// <summary>
			/// Serializes a <see cref="DObject"/>.
			/// </summary>
			/// <param name="value">The object.</param>
            /// <remarks>Avoids redundant serialization of the same object by using <see cref="serializedRefs"/>.</remarks>
			private void WriteObject(DObject value)
			{
				int seq;
				if (serializedRefs.TryGetValue(value, out seq))
				{
					// this object instance has already been serialized -> write out its seq. number
					writer.Write(Tokens.ObjectRef);
					writer.Write(Tokens.Colon);
					writer.Write(seq);
					writer.Write(Tokens.Semicolon);
					sequenceNumber--;
				}
				else
				{
                    serializedRefs.Add(value, sequenceNumber);

                    //
                    if (value.GetType() == typeof(ClrObject) || value is IClrValue)
                        WriteClrObjectInternal(value.RealObject);
                    else
                        WritePhpObjectInternal(value);
                }
            }
Exemple #5
0
 private void Format(DObject nullValue)
 {
     throw new InvalidOperationException($"Value of {nullValue.GetType().Name} is not supported, forgot (dynamic) dispatch?");
 }