Esempio n. 1
0
        // Write constructor
        internal void InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter)
        {
            SerTrace.Log(this, objectInfoId, " Constructor 1 ", obj);
            this.context           = context;
            this.obj               = obj;
            this.serObjectInfoInit = serObjectInfoInit;
            ISurrogateSelector surrogateSelectorTemp;

            if (RemotingServices.IsTransparentProxy(obj))
            {
                objectType = Converter.typeofMarshalByRefObject;
            }
            else
            {
                objectType = obj.GetType();
            }

            if (objectType.IsArray)
            {
                isArray = true;
                InitNoMembers();
                return;
            }

            SerTrace.Log(this, objectInfoId, " Constructor 1 trace 2");

            objectWriter.ObjectManager.RegisterObject(obj);
            if (surrogateSelector != null && (serializationSurrogate = surrogateSelector.GetSurrogate(objectType, context, out surrogateSelectorTemp)) != null)
            {
                SerTrace.Log(this, objectInfoId, " Constructor 1 trace 3");
                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(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Serialization_NonSerType"),
                                                                   objectType.FullName, objectType.Assembly.FullName));
                }
                si = new SerializationInfo(objectType, converter);
                ((ISerializable)obj).GetObjectData(si, context);
                SerTrace.Log(this, objectInfoId, " Constructor 1 trace 4 ISerializable " + objectType);
                InitSiWrite();
            }
            else
            {
                SerTrace.Log(this, objectInfoId, " Constructor 1 trace 5");
                InitMemberInfo();
            }
        }
Esempio n. 2
0
 internal void InitSerialize(object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerializationObjectInfo serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder)
 {
     _context           = context;
     _obj               = obj;
     _serObjectInfoInit = serObjectInfoInit;
     _objectType        = obj.GetType();
     if (_objectType.IsArray)
     {
         _isArray = true;
         InitNoMembers();
     }
     else if (_objectType.IsSerializable)
     {
         InvokeSerializationBinder(binder);
         objectWriter.ObjectManager.RegisterObject(obj);
         if (surrogateSelector != null && (_serializationSurrogate = surrogateSelector.GetSurrogate(_objectType, context, out _)) != null)
         {
             _si = new SerializationInfo(_objectType, converter);
             if (!_objectType.IsPrimitive)
             {
                 _serializationSurrogate.GetObjectData(obj, _si, context);
             }
             InitSiWrite();
         }
         else if (obj is ISerializable serializable)
         {
             if (!_objectType.IsSerializable)
             {
                 throw new SerializationException(RemotingResources.NotMarkedForSerialization.Format(_objectType.FullName, _objectType.Assembly.FullName));
             }
             _si = new SerializationInfo(_objectType, converter);
             serializable.GetObjectData(_si, context);
             InitSiWrite();
             //CheckTypeForwardedFrom(_cache, _objectType, _binderAssemblyString);
         }
         else
         {
             InitMemberInfo();
             //CheckTypeForwardedFrom(_cache, _objectType, _binderAssemblyString);
         }
     }
     else
     {
         throw new SerializationException(RemotingResources.NotMarkedForSerialization.Format(_objectType.FullName, _objectType.Assembly.FullName));
     }
 }
Esempio n. 3
0
        //Gets all the serializable members of an object and return an enumarable collection.
        private IEnumerable <SerializationEntry> GetMemberInfo(object objectToSerialize, Type objectToSerializeType, FormatterConverter converter)
        {
            ISurrogateSelector      selector1 = null;
            ISerializationSurrogate serializationSurrogate = null;
            SerializationInfo       info = null;
            Type objectType = objectToSerializeType;

            //If the passed object is null, break the iteration.
            if (objectToSerialize == null)
            {
                yield break;
            }

            //Get the serialization surrogate
            if (SurrogateSelector == null)
            {
                throw new NullReferenceException("An error occurred serializing an object. The SurrogateSelector property may not be null.");
            }
            serializationSurrogate = SurrogateSelector.GetSurrogate(objectType, Context, out selector1);
            if (serializationSurrogate == null)
            {
                throw new NullReferenceException(string.Format("An error occurred serializing an object. A surrogate was not found for type {0}", objectType.Name));
            }

            //Use the surrogate to get the members.
            info = new SerializationInfo(objectType, converter);

            //Get the data from the surrogate.
            if (!objectType.IsPrimitive)
            {
                serializationSurrogate.GetObjectData(objectToSerialize, info, Context);
            }

            //Create the custom entries collection by copying all the members
            foreach (System.Runtime.Serialization.SerializationEntry member in info)
            {
                //SerializationEntry entry = new SerializationEntry(member.Name, member.ObjectType, member.Value);

                //yield return will return the entry now and return to this point when
                //the enclosing loop (the one that contains the call to this method)
                //request the next item.
                //yield return entry;
                yield return(member);
            }
        }
Esempio n. 4
0
        private SerializationInfo getSerializationInfo(object graph, IFormatterConverter converter)
        {
            System.Diagnostics.Debug.Assert(null != graph, "The 'graph' argument cannot be null.");
            System.Diagnostics.Debug.Assert(null != converter, "The 'converter' argument cannot be null");

            Type graphType = graph.GetType();

            ISurrogateSelector      selector  = null;
            ISerializationSurrogate surrogate = this.getSerializationSurrogate(graphType, out selector);

            if (null == surrogate)
            {
                throw new SerializationException(String.Format("No SerializationSurrogate found for Type '{0}' in Assembly '{1}'", graph.GetType().FullName, graph.GetType().Assembly.FullName));
            }

            SerializationInfo graphInfo = new SerializationInfo(graphType, converter);

            surrogate.GetObjectData(graph, graphInfo, this.context);

            return(graphInfo);
        }
Esempio n. 5
0
        private void SerializeISerializableObject(
            object currentObject,
            long currentObjectId,
            ISerializationSurrogate surrogate)
        {
            Type currentType       = currentObject.GetType();
            SerializationInfo info = new SerializationInfo(currentType, new FormatterConverter());


            ISerializable objISerializable = currentObject as ISerializable;

            if (surrogate != null)
            {
                surrogate.GetObjectData(currentObject, info, _context);
            }
            else
            {
                objISerializable.GetObjectData(info, _context);
            }

            // Same as above
            if (currentObjectId > 0L)
            {
                Element element = _mapper.GetXmlElement(info.FullTypeName, info.AssemblyName);
                _xmlWriter.WriteStartElement(element.Prefix, element.LocalName, element.NamespaceURI);
                Id(currentObjectId);
            }

            foreach (SerializationEntry entry in info)
            {
                _xmlWriter.WriteStartElement(XmlConvert.EncodeLocalName(entry.Name));
                SerializeComponent(entry.Value, IsEncodingNeeded(entry.Value, null));
                _xmlWriter.WriteEndElement();
            }
            if (currentObjectId > 0)
            {
                _xmlWriter.WriteFullEndElement();
            }
        }
Esempio n. 6
0
            /// <summary>
            /// Populates the provided <see cref="SerializationInfo"/> with the data needed to
            /// serialize the object and records that the object was unconditionally written.
            /// </summary>
            /// <param name="obj">The object to serialize.</param>
            /// <param name="info">The <see cref="SerializationInfo"/> to populate with data.</param>
            /// <param name="context">
            /// The destination (see <see cref="StreamingContext"/>) for this serialization.
            /// </param>
            public void GetObjectData(Object obj, SerializationInfo info, StreamingContext context)
            {
                if (!unconditionallyWritten.Contains(obj))
                {
                    // Record that the object was unconditionally written.
                    if (!obj.GetType().IsValueType)
                    {
                        unconditionallyWritten.Add(obj, true);
                    }

                    // If there is an actual surrogate for this type of object, use the surrogate
                    // to populate the SerializationInfo.  Otherwise, if the object implements
                    // ISerializable, use the object's GetObjectData method to populate the
                    // SerializationInfo.  Otherwise, get the serializable members of the object
                    // and write them directly to the SerializationInfo.

                    if (actualSurrogate != null)
                    {
                        actualSurrogate.GetObjectData(obj, info, context);
                    }
                    else if (obj is ISerializable)
                    {
                        ((ISerializable)obj).GetObjectData(info, context);
                    }
                    else
                    {
                        // Serialize serializable members
                        Type         objType = obj.GetType();
                        MemberInfo[] mi      = FormatterServices.GetSerializableMembers(objType, context);

                        for (int i = 0; i < mi.Length; i++)
                        {
                            info.AddValue(mi[i].Name, ((FieldInfo)mi[i]).GetValue(obj));
                        }
                    }
                }
            }
        // Write constructor
        internal void InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter)
        {
            SerTrace.Log( this, objectInfoId," Constructor 1 ",obj);
            this.context = context;
            this.obj = obj;
            this.serObjectInfoInit = serObjectInfoInit;
            ISurrogateSelector surrogateSelectorTemp;

            if (RemotingServices.IsTransparentProxy(obj))
                objectType = Converter.typeofMarshalByRefObject;
            else
                objectType = obj.GetType();

            if (objectType.IsArray)
            {
                isArray = true;
                InitNoMembers();
                return;
            }

            SerTrace.Log( this, objectInfoId," Constructor 1 trace 2");

            objectWriter.ObjectManager.RegisterObject(obj);
            if (surrogateSelector != null && (serializationSurrogate = surrogateSelector.GetSurrogate(objectType, context, out surrogateSelectorTemp)) != null)
            {
                SerTrace.Log( this, objectInfoId," Constructor 1 trace 3");
                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(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Serialization_NonSerType"),
                                                                   objectType.FullName, objectType.Assembly.FullName));
                }
                si = new SerializationInfo(objectType, converter);
                ((ISerializable)obj).GetObjectData(si, context);
                SerTrace.Log( this, objectInfoId," Constructor 1 trace 4 ISerializable "+objectType);
                InitSiWrite();
            }
            else
            {
                SerTrace.Log(this, objectInfoId," Constructor 1 trace 5");
                InitMemberInfo();
            }
        }
Esempio n. 8
0
 /// <summary>
 /// Write the specified value.
 /// </summary>
 /// <param name="value">Value.</param>
 public override void Write(object value)
 {
     if (value == null)
     {
         m_Writer.Write("null");
     }
     else
     {
         Type type = value.GetType();
         if (type == typeof(string) || type.IsEnum)
         {
             m_Writer.Write("\"{0}\"", value);
         }
         else if (type == typeof(short) || type == typeof(int) || type == typeof(long) ||
                  type == typeof(ushort) || type == typeof(uint) || type == typeof(ulong) ||
                  type == typeof(byte) || type == typeof(sbyte) || type == typeof(decimal) ||
                  type == typeof(double) || type == typeof(float))
         {
             m_Writer.Write(Convert.ChangeType(value, typeof(string)));
         }
         else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(KeyValuePair <,>))
         {
             m_Writer.Write("{");
             Write("Key");
             m_Writer.Write(":");
             Write(type.GetProperty("Key").GetValue(value, BindingFlags.Default, null, null, null));
             m_Writer.Write(",");
             Write("Value");
             m_Writer.Write(":");
             Write(type.GetProperty("Value").GetValue(value, BindingFlags.Default, null, null, null));
             m_Writer.Write("}");
         }
         else if (value is IDictionary)
         {
             IDictionary dictionary = value as IDictionary;
             bool        isFirst    = true;
             m_Writer.Write("{");
             foreach (var key in dictionary.Keys)
             {
                 if (isFirst)
                 {
                     isFirst = false;
                 }
                 else
                 {
                     m_Writer.Write(",");
                 }
                 Write(key);
                 m_Writer.Write(":");
                 Write(dictionary[key]);
             }
             m_Writer.Write("}");
         }
         else if (value is IEnumerable)
         {
             IEnumerable enumerable = value as IEnumerable;
             IEnumerator e          = enumerable.GetEnumerator();
             bool        isFirst    = true;
             m_Writer.Write("[");
             while (e.MoveNext())
             {
                 if (isFirst)
                 {
                     isFirst = false;
                 }
                 else
                 {
                     m_Writer.Write(",");
                 }
                 Write(e.Current);
             }
             m_Writer.Write("]");
         }
         else if (value is ISerializable)
         {
             SerializationInfo info         = new SerializationInfo(type, new FormatterConverter());
             ISerializable     serializable = value as ISerializable;
             serializable.GetObjectData(info, m_Context);
             WriteSerializationInfo(info);
         }
         else
         {
             if (m_SurrogateSelector != null)
             {
                 ISurrogateSelector      selector;
                 ISerializationSurrogate surrogate = m_SurrogateSelector.GetSurrogate(type, m_Context, out selector);
                 if (surrogate != null)
                 {
                     SerializationInfo info = new SerializationInfo(type, new FormatterConverter());
                     surrogate.GetObjectData(value, info, m_Context);
                     WriteSerializationInfo(info);
                 }
             }
             else
             {
                 WriteObject(value, type);
             }
         }
     }
 }
Esempio n. 9
0
        // Write constructor
        // [System.Security.SecurityCritical]  // auto-generated
        internal void InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder)
        {
            SerTrace.Log(this, objectInfoId, " Constructor 1 ", obj);
            this.context           = context;
            this.obj               = obj;
            this.serObjectInfoInit = serObjectInfoInit;
            ISurrogateSelector surrogateSelectorTemp;

#if  FEATURE_REMOTING
            if (RemotingServices.IsTransparentProxy(obj))
            {
                objectType = Converter.typeofMarshalByRefObject;
            }
            else
#endif
            objectType = obj.GetType();

            if (objectType.IsArray)
            {
                isArray = true;
                InitNoMembers();
                return;
            }

            InvokeSerializationBinder(binder);

            SerTrace.Log(this, objectInfoId, " Constructor 1 trace 2");

            objectWriter.ObjectManager.RegisterObject(obj);
            if (surrogateSelector != null && (serializationSurrogate = surrogateSelector.GetSurrogate(objectType, context, out surrogateSelectorTemp)) != null)
            {
                SerTrace.Log(this, objectInfoId, " Constructor 1 trace 3");
                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(Ssz.Runtime.Serialization.Environment.GetResourceString("Serialization_NonSerType",
                                                                                                             objectType.FullName, objectType.Assembly.FullName));
                }
                si = new SerializationInfo(objectType, converter, !FormatterServices.UnsafeTypeForwardersIsEnabled());
#if FEATURE_SERIALIZATION
                ((ISerializable)obj).GetObjectData(si, context);
#endif
                SerTrace.Log(this, objectInfoId, " Constructor 1 trace 4 ISerializable " + objectType);
                InitSiWrite();
                CheckTypeForwardedFrom(cache, objectType, binderAssemblyString);
            }
            else
            {
                SerTrace.Log(this, objectInfoId, " Constructor 1 trace 5");
                InitMemberInfo();
                CheckTypeForwardedFrom(cache, objectType, binderAssemblyString);
            }
        }
Esempio n. 10
0
        private void GetObjectData(object obj, out TypeMetadata metadata, out object data)
        {
            Type instanceType = obj.GetType();

            // Check if the formatter has a surrogate selector, if it does,
            // check if the surrogate selector handles objects of the given type.

            if (_surrogateSelector != null)
            {
                ISurrogateSelector      selector;
                ISerializationSurrogate surrogate = _surrogateSelector.GetSurrogate(instanceType, _context, out selector);
                if (surrogate != null)
                {
                    SerializationInfo info = new SerializationInfo(instanceType, new FormatterConverter());
                    surrogate.GetObjectData(obj, info, _context);
                    metadata = new SerializableTypeMetadata(instanceType, info);
                    data     = info;
                    return;
                }
            }

            // Check if the object is marked with the Serializable attribute

            BinaryCommon.CheckSerializable(instanceType, _surrogateSelector, _context);

#if NET_2_0
            _manager.RegisterObject(obj);
#endif

            ISerializable ser = obj as ISerializable;

            if (ser != null)
            {
                SerializationInfo info = new SerializationInfo(instanceType, new FormatterConverter());
                ser.GetObjectData(info, _context);
                metadata = new SerializableTypeMetadata(instanceType, info);
                data     = info;
            }
            else
            {
                data = obj;
                if (_context.Context != null)
                {
                    // Don't cache metadata info when the Context property is not null sice
                    // we can't control the number of possible contexts in this case
                    metadata = new MemberTypeMetadata(instanceType, _context);
                    return;
                }

                Hashtable typesTable;
                bool      isNew = false;
                lock (_cachedTypes) {
                    typesTable = (Hashtable)_cachedTypes [_context.State];
                    if (typesTable == null)
                    {
                        typesTable = new Hashtable();
                        _cachedTypes [_context.State] = typesTable;
                        isNew = true;
                    }
                }

                metadata = null;
                lock (typesTable) {
                    if (!isNew)
                    {
                        metadata = (TypeMetadata)typesTable [instanceType];
                    }

                    if (metadata == null)
                    {
                        metadata = CreateMemberTypeMetadata(instanceType);
                    }

                    typesTable [instanceType] = metadata;
                }
            }
        }
        [System.Security.SecurityCritical]  // auto-generated
        internal void InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder)
        {
            SerTrace.Log( this, objectInfoId," Constructor 1 ",obj);
            this.context = context;
            this.obj = obj;
            this.serObjectInfoInit = serObjectInfoInit;
            ISurrogateSelector surrogateSelectorTemp;

#if  FEATURE_REMOTING        
            if (RemotingServices.IsTransparentProxy(obj))
                objectType = Converter.typeofMarshalByRefObject;
            else
#endif
                objectType = obj.GetType();

            if (objectType.IsArray)
            {
                isArray = true;
                InitNoMembers();
                return;
            }

            InvokeSerializationBinder(binder);

            SerTrace.Log( this, objectInfoId," Constructor 1 trace 2");

            objectWriter.ObjectManager.RegisterObject(obj);
            if (surrogateSelector != null && (serializationSurrogate = surrogateSelector.GetSurrogate(objectType, context, out surrogateSelectorTemp)) != null)
            {
                SerTrace.Log( this, objectInfoId," Constructor 1 trace 3");
                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(Environment.GetResourceString("Serialization_NonSerType",
                                                                   objectType.FullName, objectType.Assembly.FullName));
                }
                si = new SerializationInfo(objectType, converter, !FormatterServices.UnsafeTypeForwardersIsEnabled());
#if FEATURE_SERIALIZATION
                ((ISerializable)obj).GetObjectData(si, context);
#endif
                SerTrace.Log( this, objectInfoId," Constructor 1 trace 4 ISerializable "+objectType);
                InitSiWrite();
                CheckTypeForwardedFrom(cache, objectType, binderAssemblyString);
            }
            else
            {
                SerTrace.Log(this, objectInfoId," Constructor 1 trace 5");
                InitMemberInfo();
                CheckTypeForwardedFrom(cache, objectType, binderAssemblyString);
            }
        }
Esempio n. 12
0
        internal static object GetObjectData(object serObj, out string typeName, out string assemName, out string[] fieldNames, out object[] fieldValues)
        {
            Type   type = null;
            object obj2 = null;

            if (RemotingServices.IsTransparentProxy(serObj))
            {
                type = typeof(MarshalByRefObject);
            }
            else
            {
                type = serObj.GetType();
            }
            SerializationInfo info = new SerializationInfo(type, s_converter);

            if (serObj is ObjRef)
            {
                s_ObjRefRemotingSurrogate.GetObjectData(serObj, info, s_cloneContext);
            }
            else if (RemotingServices.IsTransparentProxy(serObj) || (serObj is MarshalByRefObject))
            {
                if (!RemotingServices.IsTransparentProxy(serObj) || (RemotingServices.GetRealProxy(serObj) is RemotingProxy))
                {
                    ObjRef ref2 = RemotingServices.MarshalInternal((MarshalByRefObject)serObj, null, null);
                    if (ref2.CanSmuggle())
                    {
                        if (RemotingServices.IsTransparentProxy(serObj))
                        {
                            RealProxy realProxy = RemotingServices.GetRealProxy(serObj);
                            ref2.SetServerIdentity(realProxy._srvIdentity);
                            ref2.SetDomainID(realProxy._domainID);
                        }
                        else
                        {
                            ServerIdentity identity = (ServerIdentity)MarshalByRefObject.GetIdentity((MarshalByRefObject)serObj);
                            identity.SetHandle();
                            ref2.SetServerIdentity(identity.GetHandle());
                            ref2.SetDomainID(AppDomain.CurrentDomain.GetId());
                        }
                        ref2.SetMarshaledObject();
                        obj2 = ref2;
                    }
                }
                if (obj2 == null)
                {
                    s_RemotingSurrogate.GetObjectData(serObj, info, s_cloneContext);
                }
            }
            else
            {
                if (!(serObj is ISerializable))
                {
                    throw new ArgumentException(Environment.GetResourceString("Arg_SerializationException"));
                }
                ((ISerializable)serObj).GetObjectData(info, s_cloneContext);
            }
            if (obj2 == null)
            {
                typeName    = info.FullTypeName;
                assemName   = info.AssemblyName;
                fieldNames  = info.MemberNames;
                fieldValues = info.MemberValues;
                return(obj2);
            }
            typeName    = null;
            assemName   = null;
            fieldNames  = null;
            fieldValues = null;
            return(obj2);
        }
Esempio n. 13
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);
                }
            }
        }
Esempio n. 14
0
        /// <summary>
        /// Gets all the serializable members of an object and return an enumarable collection.
        /// </summary>
        /// <param name="objectToSerialize">The object to get the members from.</param>
        /// <param name="objectToSerializeType">The type of the object.</param>
        /// <param name="converter">The converter to use when converting simple types.</param>
        /// <returns>An IEnumerable list of <see cref="CustomSerializationEntry"/> entries.</returns>
        private IEnumerable <CustomSerializationEntry> GetMemberInfo(object objectToSerialize, Type objectToSerializeType, FormatterConverter converter)
        {
            ISurrogateSelector      selector1 = null;
            ISerializationSurrogate serializationSurrogate = null;
            SerializationInfo       info = null;
            Type objectType = objectToSerializeType;

            // if the passed object is null, break the iteration.
            if (objectToSerialize == null)
            {
                yield break;
            }

            if ((SurrogateSelector != null) && ((serializationSurrogate = SurrogateSelector.GetSurrogate(objectType, Context, out selector1)) != null))
            {
                // use a surrogate to get the members.
                info = new SerializationInfo(objectType, converter);

                if (!objectType.IsPrimitive)
                {
                    // get the data from the surrogate.
                    serializationSurrogate.GetObjectData(objectToSerialize, info, Context);
                }
            }
            else if (objectToSerialize is ISerializable)
            {
                // object implements ISerializable
                if (!objectType.IsSerializable)
                {
                    throw new SerializationException(string.Format("Type is not serializable : {0}.", objectType.FullName));
                }

                info = new SerializationInfo(objectType, converter);
                // get the data using ISerializable.
                ((ISerializable)objectToSerialize).GetObjectData(info, Context);
            }

            if (info != null)
            {
                // either the surrogate provided the members, or the members were retrieved
                // using ISerializable.
                // create the custom entries collection by copying all the members
                foreach (SerializationEntry member in info)
                {
                    CustomSerializationEntry entry = new CustomSerializationEntry(member.Name, member.ObjectType, false, member.Value);
                    // yield return will return the entry now and return to this point when
                    // the enclosing loop (the one that contains the call to this method)
                    // request the next item.
                    yield return(entry);
                }
            }
            else
            {
                // The item does not hava surrogate, not does it implement ISerializable.
                // We use reflection to get the objects state.
                if (!objectType.IsSerializable)
                {
                    throw new SerializationException(string.Format("Type is not serializable : {0}.", objectType.FullName));
                }
                // Get all serializable members
                MemberInfo[] members = FormatterServices.GetSerializableMembers(objectType, Context);

                foreach (MemberInfo member in members)
                {
                    if (CanSerialize(member))
                    {
                        // create the entry
                        CustomSerializationEntry entry = new CustomSerializationEntry(member.Name, member.ReflectedType, false);

                        if (typeof(IList).IsAssignableFrom(entry.ObjectType))
                        {
                            entry.IsList = true;
                        }
                        // get the value of the member
                        entry.Value = GetMemberValue(objectToSerialize, member);
                        // yield return will return the entry now and return to this point when
                        // the enclosing loop (the one that contains the call to this method)
                        // request the next item.
                        yield return(entry);
                    }
                }
            }
        }
Esempio n. 15
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);
            }
        }
Esempio n. 16
0
 public void GetObjectData(object obj, SerializationInfo info, StreamingContext context)
 {
     _innerSurrogate.GetObjectData(obj, info, context);
 }
Esempio n. 17
0
 /// <summary>
 /// Write the specified value.
 /// </summary>
 /// <param name="value">Value.</param>
 public virtual void Write(object value)
 {
     if (value == null)
     {
         m_Writer.Write(0);
     }
     else
     {
         Type type = value.GetType();
         if (type.IsPrimitive || type == typeof(string) || type == typeof(decimal))
         {
             if (type == typeof(string))
             {
                 m_Writer.Write(( string )value);
             }
             else if (type == typeof(decimal))
             {
                 m_Writer.Write(( decimal )value);
             }
             else if (type == typeof(short))
             {
                 m_Writer.Write(( short )value);
             }
             else if (type == typeof(int))
             {
                 m_Writer.Write(( int )value);
             }
             else if (type == typeof(long))
             {
                 m_Writer.Write(( long )value);
             }
             else if (type == typeof(ushort))
             {
                 m_Writer.Write(( ushort )value);
             }
             else if (type == typeof(uint))
             {
                 m_Writer.Write(( uint )value);
             }
             else if (type == typeof(ulong))
             {
                 m_Writer.Write(( ulong )value);
             }
             else if (type == typeof(double))
             {
                 m_Writer.Write(( double )value);
             }
             else if (type == typeof(float))
             {
                 m_Writer.Write(( float )value);
             }
             else if (type == typeof(byte))
             {
                 m_Writer.Write(( byte )value);
             }
             else if (type == typeof(sbyte))
             {
                 m_Writer.Write(( sbyte )value);
             }
             else if (type == typeof(char))
             {
                 m_Writer.Write(( char )value);
             }
             else if (type == typeof(bool))
             {
                 m_Writer.Write(( bool )value);
             }
         }
         else if (type.IsEnum)
         {
             m_Writer.Write(value.ToString());
         }
         else if (type == typeof(DateTime))
         {
             m_Writer.Write((( DateTime )value).ToBinary());
         }
         else if (type == typeof(TimeSpan))
         {
             m_Writer.Write((( TimeSpan )value).ToString());
         }
         else if (value is ISerializable)
         {
             SerializationInfo info         = new SerializationInfo(type, new FormatterConverter());
             ISerializable     serializable = value as ISerializable;
             serializable.GetObjectData(info, m_Context);
             WriteSerializationInfo(info);
         }
         else if (type.IsSerializable && !type.IsArray)
         {
             WriteObject(value, type);
         }
         else if (type.IsArray)
         {
             Array array = value as Array;
             m_Writer.Write(array.Rank);
             for (int i = 0; i < array.Rank; i++)
             {
                 m_Writer.Write(array.GetLength(i));
             }
             int [] indices = new int[array.Rank];
             for (int i = 0; i < array.Rank; i++)
             {
                 for (int j = 0; j < array.GetLength(i); j++)
                 {
                     indices [i] = j;
                     Write(array.GetValue(indices));
                 }
             }
         }
         else if (value is IEnumerable && value is ICollection)
         {
             IEnumerable enumerable = value as IEnumerable;
             ICollection collection = value as ICollection;
             IEnumerator e          = enumerable.GetEnumerator();
             m_Writer.Write(collection.Count);
             while (e.MoveNext())
             {
                 Write(e.Current);
             }
         }
         else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(KeyValuePair <,>))
         {
             Write(type.GetProperty("Key").GetValue(value, BindingFlags.Default, null, null, null));
             Write(type.GetProperty("Value").GetValue(value, BindingFlags.Default, null, null, null));
         }
         else
         {
             ISerializationSurrogate surrogate = null;
             if (m_SurrogateSelector != null)
             {
                 ISurrogateSelector selector;
                 surrogate = m_SurrogateSelector.GetSurrogate(type, m_Context, out selector);
                 if (surrogate != null)
                 {
                     SerializationInfo info = new SerializationInfo(type, new FormatterConverter());
                     surrogate.GetObjectData(value, info, m_Context);
                     WriteSerializationInfo(info);
                 }
             }
             if (surrogate == null)
             {
                 WriteObject(value, type);
             }
         }
     }
 }
Esempio n. 18
0
		private void SerializeISerializableObject(
			object currentObject,
			long currentObjectId,
			ISerializationSurrogate surrogate)
		{
			Type currentType = currentObject.GetType();
			SerializationInfo info = new SerializationInfo(currentType, new FormatterConverter());


			ISerializable objISerializable = currentObject as ISerializable;
			if(surrogate != null) surrogate.GetObjectData(currentObject, info, _context);
			else
			{
				objISerializable.GetObjectData(info, _context);
			}

			// Same as above
			if(currentObjectId > 0L)
			{
				Element element = _mapper. GetXmlElement (info.FullTypeName, info.AssemblyName);
				_xmlWriter.WriteStartElement(element.Prefix, element.LocalName, element.NamespaceURI);
				Id(currentObjectId);
			}

			foreach(SerializationEntry entry in info)
			{
				_xmlWriter.WriteStartElement(XmlConvert.EncodeLocalName (entry.Name));
				SerializeComponent(entry.Value, IsEncodingNeeded(entry.Value, null));
				_xmlWriter.WriteEndElement();
			}
			if(currentObjectId > 0)
				_xmlWriter.WriteFullEndElement();

		}
Esempio n. 19
0
        // Write constructor
        internal void InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, SoapAttributeInfo attributeInfo)
        {
            InternalST.Soap( this, objectInfoId," Constructor 1 ",obj);        
            this.context = context;
            this.obj = obj;
            this.serObjectInfoInit = serObjectInfoInit;
            this.parentMemberAttributeInfo = attributeInfo;
            this.surrogateSelector = surrogateSelector;
            this.converter = converter;
            ISurrogateSelector surrogateSelectorTemp;

            if (RemotingServices.IsTransparentProxy(obj))
                objectType = Converter.typeofMarshalByRefObject;
            else
                objectType = obj.GetType();

            if (objectType.IsArray)
            {
                arrayElemObjectInfo = Serialize(objectType.GetElementType(), surrogateSelector, context, serObjectInfoInit, converter, null);
                typeAttributeInfo = GetTypeAttributeInfo();
                isArray = true;
                InitNoMembers();
                return;
            }

            InternalST.Soap( this, objectInfoId," Constructor 1 trace 2");

            typeAttributeInfo = GetTypeAttributeInfo();

            if (surrogateSelector != null && (serializationSurrogate = surrogateSelector.GetSurrogate(objectType, context, out surrogateSelectorTemp)) != null)
            {
                InternalST.Soap( this, objectInfoId," Constructor 1 trace 3");         
                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(String.Format(SoapUtil.GetResourceString("Serialization_NonSerType"), 
                                                                   objectType.FullName, objectType.Module.Assembly.FullName));
                }
                si = new SerializationInfo(objectType, converter);
                ((ISerializable)obj).GetObjectData(si, context);
                InternalST.Soap( this, objectInfoId," Constructor 1 trace 4 ISerializable "+objectType);                       
                InitSiWrite();
            }
            else
            {
                InternalST.Soap(this, objectInfoId," Constructor 1 trace 5");
                InitMemberInfo();
            }
        }
Esempio n. 20
0
        [System.Security.SecurityCritical]  // auto-generated
        internal void InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder)
        {
            SerTrace.Log(this, objectInfoId, " Constructor 1 ", obj);
            this.context           = context;
            this.obj               = obj;
            this.serObjectInfoInit = serObjectInfoInit;
            ISurrogateSelector surrogateSelectorTemp;

#if  FEATURE_REMOTING
            if (RemotingServices.IsTransparentProxy(obj))
            {
                objectType = Converter.typeofMarshalByRefObject;
            }
            else
#endif
            objectType = obj.GetType();

            if (objectType.IsArray)
            {
                isArray = true;
                InitNoMembers();
                return;
            }

            InvokeSerializationBinder(binder);

            SerTrace.Log(this, objectInfoId, " Constructor 1 trace 2");

            objectWriter.ObjectManager.RegisterObject(obj);
            if (surrogateSelector != null && (serializationSurrogate = surrogateSelector.GetSurrogate(objectType, context, out surrogateSelectorTemp)) != null)
            {
                SerTrace.Log(this, objectInfoId, " Constructor 1 trace 3");
                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(Environment.GetResourceString("Serialization_NonSerType",
                                                                                   objectType.FullName, objectType.Assembly.FullName));
                }
                Assembly mscorlibAsm                         = Assembly.Load("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
                var      FormatterServicesRef                = mscorlibAsm.GetType("System.Runtime.Serialization.FormatterServices");
                var      unsafeTypeForwardersIsEnabled       = FormatterServicesRef.GetMethod("UnsafeTypeForwardersIsEnabled", BindingFlags.Static | BindingFlags.NonPublic);
                var      unsafeTypeForwardersIsEnabledResult = (bool)unsafeTypeForwardersIsEnabled.Invoke(null, new object[] { });
                si = new SerializationInfo(objectType, converter, !unsafeTypeForwardersIsEnabledResult);
                //si = new SerializationInfo(objectType, converter, !FormatterServices.UnsafeTypeForwardersIsEnabled());
//#if FEATURE_SERIALIZATION
                ((ISerializable)obj).GetObjectData(si, context);
//#endif
                SerTrace.Log(this, objectInfoId, " Constructor 1 trace 4 ISerializable " + objectType);
                InitSiWrite();
                CheckTypeForwardedFrom(cache, objectType, binderAssemblyString);
            }
            else
            {
                SerTrace.Log(this, objectInfoId, " Constructor 1 trace 5");
                InitMemberInfo();
                CheckTypeForwardedFrom(cache, objectType, binderAssemblyString);
            }
        }
Esempio n. 21
0
        [System.Security.SecurityCritical]  // auto-generated
        internal static object GetObjectData(object serObj, out string typeName, out string assemName, out string[] fieldNames, out object[] fieldValues)
        {
            Type   objectType = null;
            object retObj     = null;

            if (RemotingServices.IsTransparentProxy(serObj))
            {
                objectType = typeof(MarshalByRefObject);
            }
            else
            {
                objectType = serObj.GetType();
            }

            SerializationInfo si = new SerializationInfo(objectType, s_converter);

            if (serObj is ObjRef)
            {
                s_ObjRefRemotingSurrogate.GetObjectData(serObj, si, s_cloneContext);
            }
            else if (RemotingServices.IsTransparentProxy(serObj) || serObj is MarshalByRefObject)
            {
#if !MONO
                // We can only try to smuggle objref's for actual CLR objects
                //   or for RemotingProxy's.
                if (!RemotingServices.IsTransparentProxy(serObj) ||
                    RemotingServices.GetRealProxy(serObj) is RemotingProxy)
                {
                    ObjRef objRef = RemotingServices.MarshalInternal((MarshalByRefObject)serObj, null, null);
                    if (objRef.CanSmuggle())
                    {
                        if (RemotingServices.IsTransparentProxy(serObj))
                        {
                            RealProxy rp = RemotingServices.GetRealProxy(serObj);
                            objRef.SetServerIdentity(rp._srvIdentity);
                            objRef.SetDomainID(rp._domainID);
                        }
                        else
                        {
                            ServerIdentity srvId = (ServerIdentity)MarshalByRefObject.GetIdentity((MarshalByRefObject)serObj);
                            srvId.SetHandle();
                            objRef.SetServerIdentity(srvId.GetHandle());
                            objRef.SetDomainID(AppDomain.CurrentDomain.GetId());
                        }
                        objRef.SetMarshaledObject();
                        retObj = objRef;
                    }
                }
#endif
                if (retObj == null)
                {
                    // Deal with the non-smugglable remoting objects
                    s_RemotingSurrogate.GetObjectData(serObj, si, s_cloneContext);
                }
            }
            else if (serObj is ISerializable)
            {
                ((ISerializable)serObj).GetObjectData(si, s_cloneContext);
            }
            else
            {
                // Getting here means a bug in cloner
                throw new ArgumentException(Environment.GetResourceString("Arg_SerializationException"));
            }

            if (retObj == null)
            {
                typeName    = si.FullTypeName;
                assemName   = si.AssemblyName;
                fieldNames  = si.MemberNames;
                fieldValues = si.MemberValues;
            }
            else
            {
                typeName    = null;
                assemName   = null;
                fieldNames  = null;
                fieldValues = null;
            }

            return(retObj);
        }
Esempio n. 22
0
        private void GetObjectData(object obj, out TypeMetadata metadata, out object data)
        {
            Type type = obj.GetType();

            if (this._surrogateSelector != null)
            {
                ISurrogateSelector      surrogateSelector;
                ISerializationSurrogate surrogate = this._surrogateSelector.GetSurrogate(type, this._context, out surrogateSelector);
                if (surrogate != null)
                {
                    SerializationInfo serializationInfo = new SerializationInfo(type, new FormatterConverter());
                    surrogate.GetObjectData(obj, serializationInfo, this._context);
                    metadata = new SerializableTypeMetadata(type, serializationInfo);
                    data     = serializationInfo;
                    return;
                }
            }
            BinaryCommon.CheckSerializable(type, this._surrogateSelector, this._context);
            this._manager.RegisterObject(obj);
            ISerializable serializable = obj as ISerializable;

            if (serializable != null)
            {
                SerializationInfo serializationInfo2 = new SerializationInfo(type, new FormatterConverter());
                serializable.GetObjectData(serializationInfo2, this._context);
                metadata = new SerializableTypeMetadata(type, serializationInfo2);
                data     = serializationInfo2;
            }
            else
            {
                data = obj;
                if (this._context.Context != null)
                {
                    metadata = new MemberTypeMetadata(type, this._context);
                    return;
                }
                bool      flag        = false;
                Hashtable cachedTypes = ObjectWriter._cachedTypes;
                Hashtable hashtable;
                lock (cachedTypes)
                {
                    hashtable = (Hashtable)ObjectWriter._cachedTypes[this._context.State];
                    if (hashtable == null)
                    {
                        hashtable = new Hashtable();
                        ObjectWriter._cachedTypes[this._context.State] = hashtable;
                        flag = true;
                    }
                }
                metadata = null;
                Hashtable obj2 = hashtable;
                lock (obj2)
                {
                    if (!flag)
                    {
                        metadata = (TypeMetadata)hashtable[type];
                    }
                    if (metadata == null)
                    {
                        metadata = this.CreateMemberTypeMetadata(type);
                    }
                    hashtable[type] = metadata;
                }
            }
        }