Example #1
0
		private void ReadObjectContent (BinaryReader reader, TypeMetadata metadata, long objectId, out object objectInstance, out SerializationInfo info)
		{
#if NET_1_1
			if (_filterLevel == TypeFilterLevel.Low)
				objectInstance = FormatterServices.GetSafeUninitializedObject (metadata.Type);
			else
#endif
				objectInstance = FormatterServices.GetUninitializedObject (metadata.Type);
#if NET_2_0
			_manager.RaiseOnDeserializingEvent (objectInstance);
#endif
				
			info = metadata.NeedsSerializationInfo ? new SerializationInfo(metadata.Type, new FormatterConverter()) : null;

   			if (metadata.MemberNames != null)
				for (int n=0; n<metadata.FieldCount; n++)
					ReadValue (reader, objectInstance, objectId, info, metadata.MemberTypes[n], metadata.MemberNames[n], null, null);
			else
				for (int n=0; n<metadata.FieldCount; n++)
					ReadValue (reader, objectInstance, objectId, info, metadata.MemberTypes[n], metadata.MemberInfos[n].Name, metadata.MemberInfos[n], null);
		}
Example #2
0
		private TypeMetadata ReadTypeMetadata (BinaryReader reader, bool isRuntimeObject, bool hasTypeInfo)
		{
			TypeMetadata metadata = new TypeMetadata();

			string className = reader.ReadString ();
			int fieldCount = reader.ReadInt32 ();

			Type[] types = new Type[fieldCount];
			string[] names = new string[fieldCount];

			for (int n=0; n<fieldCount; n++)
				names [n] = reader.ReadString ();

			if (hasTypeInfo)
			{
				TypeTag[] codes = new TypeTag[fieldCount];

				for (int n=0; n<fieldCount; n++)
					codes [n] = (TypeTag) reader.ReadByte ();
	
				for (int n=0; n<fieldCount; n++)
					types [n] = ReadType (reader, codes[n]);
			}
			
			// Gets the type

			if (!isRuntimeObject) 
			{
				long assemblyId = (long)reader.ReadUInt32();
				metadata.Type = GetDeserializationType (assemblyId, className);
			}
			else
				metadata.Type = Type.GetType (className, true);

			metadata.MemberTypes = types;
			metadata.MemberNames = names;
			metadata.FieldCount = names.Length;

			// Now check if this objects needs a SerializationInfo struct for deserialziation.
			// SerializationInfo is needed if the object has to be deserialized using
			// a serialization surrogate, or if it implements ISerializable.

			if (_surrogateSelector != null)
			{
				// check if the surrogate selector handles objects of the given type. 
				ISurrogateSelector selector;
				ISerializationSurrogate surrogate = _surrogateSelector.GetSurrogate (metadata.Type, _context, out selector);
				metadata.NeedsSerializationInfo = (surrogate != null);
			}

			if (!metadata.NeedsSerializationInfo)
			{
				// Check if the object is marked with the Serializable attribute

				if (!metadata.Type.IsSerializable)
					throw new SerializationException("Serializable objects must be marked with the Serializable attribute");

				metadata.NeedsSerializationInfo = typeof (ISerializable).IsAssignableFrom (metadata.Type);
				if (!metadata.NeedsSerializationInfo)
				{
					metadata.MemberInfos = new MemberInfo [fieldCount];
					for (int n=0; n<fieldCount; n++)
					{
						FieldInfo field = null;
						string memberName = names[n];
						
						int i = memberName.IndexOf ('+');
						if (i != -1) {
							string baseTypeName = names[n].Substring (0,i);
							memberName = names[n].Substring (i+1);
							Type t = metadata.Type.BaseType;
							while (t != null) {
								if (t.Name == baseTypeName) {
									field = t.GetField (memberName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
									break;
								}
								else
									t = t.BaseType;
							}
						}
						else
							field = metadata.Type.GetField (memberName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
							
						if (field == null) throw new SerializationException ("Field \"" + names[n] + "\" not found in class " + metadata.Type.FullName);
						metadata.MemberInfos [n] = field;
						
						if (!hasTypeInfo) {
							types [n] = field.FieldType;
						}
					}
					metadata.MemberNames = null;	// Info now in MemberInfos
				}
			}

			// Registers the type's metadata so it can be reused later if
			// a RefTypeObject element is found

			if (!_typeMetadataCache.ContainsKey (metadata.Type))
				_typeMetadataCache [metadata.Type] = metadata;

			return metadata;
		}
		private void GetObjectData (object obj, out TypeMetadata metadata, out object data)
		{
			Type instanceType = obj.GetType();
#if NET_4_0
			string binderAssemblyName = null;
			string binderTypeName = null;
			if (_binder != null)
				_binder.BindToName (instanceType, out binderAssemblyName, out binderTypeName);
#endif
			// 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);
#if NET_4_0
					if (_binder != null)
						metadata.BindToName (binderAssemblyName, binderTypeName);
#endif

					data = info;
					return;
				}
			}

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

			BinaryCommon.CheckSerializable (instanceType, _surrogateSelector, _context);

			_manager.RegisterObject (obj);

			ISerializable ser = obj as ISerializable;

			if (ser != null) 
			{
				SerializationInfo info = new SerializationInfo (instanceType, new FormatterConverter ());
				ser.GetObjectData (info, _context);
				metadata = new SerializableTypeMetadata (instanceType, info);
#if NET_4_0
				if (_binder != null)
					metadata.BindToName (binderAssemblyName, binderTypeName);
#endif

				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);
#if NET_4_0
					if (_binder != null)
						metadata.BindToName (binderAssemblyName, binderTypeName);
#endif

					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);
#if NET_4_0
						if (_binder != null)
							metadata.BindToName (binderAssemblyName, binderTypeName);
#endif
					}

					typesTable [instanceType] = metadata;
				}
			}
		}
		public virtual bool IsCompatible (TypeMetadata other)
		{
			return true;
		}
		public override bool IsCompatible (TypeMetadata other)
		{
			if (!(other is SerializableTypeMetadata)) return false;
			
			SerializableTypeMetadata tm = (SerializableTypeMetadata)other;
			if (types.Length != tm.types.Length) return false;
			if (TypeAssemblyName != tm.TypeAssemblyName) return false;
			if (InstanceTypeName != tm.InstanceTypeName) return false;
			for (int n=0; n<types.Length; n++)
			{
				if (types[n] != tm.types[n]) return false;
				if (names[n] != tm.names[n]) return false;
			}
			return true;
		}
			public MetadataReference (TypeMetadata metadata, long id)
			{
				Metadata = metadata;
				ObjectID = id;
			}
Example #7
0
 public virtual bool IsCompatible(TypeMetadata other)
 {
     return(true);
 }
Example #8
0
		private void ReadObjectContent (BinaryReader reader, TypeMetadata metadata, long objectId, out object objectInstance, out SerializationInfo info)
		{
			if (_filterLevel == TypeFilterLevel.Low)
				objectInstance = FormatterServices.GetSafeUninitializedObject (metadata.Type);
			else
				objectInstance = FormatterServices.GetUninitializedObject (metadata.Type);
			_manager.RaiseOnDeserializingEvent (objectInstance);
				
			info = metadata.NeedsSerializationInfo ? new SerializationInfo(metadata.Type, new FormatterConverter()) : null;

			if (metadata.MemberNames != null) {
				for (int n=0; n<metadata.FieldCount; n++)
					ReadValue (reader, objectInstance, objectId, info, metadata.MemberTypes[n], metadata.MemberNames[n], null, null);
			} else
				for (int n=0; n<metadata.FieldCount; n++) {
					if (metadata.MemberInfos [n] != null)
						ReadValue (reader, objectInstance, objectId, info, metadata.MemberTypes[n], metadata.MemberInfos[n].Name, metadata.MemberInfos[n], null);
					else if (BinaryCommon.IsPrimitive(metadata.MemberTypes[n])) {
						// Since the member info is null, the type in this
						// domain does not have this type. Even though we
						// are not going to store the value, we will read
						// it from the stream so that we can advance to the
						// next block.
						ReadPrimitiveTypeValue (reader,	metadata.MemberTypes[n]);
					}
				}
		}
        private TypeMetadata ReadTypeMetadata(BinaryReader reader, bool isRuntimeObject, bool hasTypeInfo)
        {
            TypeMetadata metadata = new TypeMetadata();

            string className  = reader.ReadString();
            int    fieldCount = reader.ReadInt32();

            Type[]   types = new Type[fieldCount];
            string[] names = new string[fieldCount];

            for (int n = 0; n < fieldCount; n++)
            {
                names [n] = reader.ReadString();
            }

            if (hasTypeInfo)
            {
                TypeTag[] codes = new TypeTag[fieldCount];

                for (int n = 0; n < fieldCount; n++)
                {
                    codes [n] = (TypeTag)reader.ReadByte();
                }

                for (int n = 0; n < fieldCount; n++)
                {
                    Type t = ReadType(reader, codes[n], false);
                    // The field's type could not be resolved: assume it is an object.
                    if (t == null)
                    {
                        t = typeof(object);
                    }
                    types [n] = t;
                }
            }

            // Gets the type

            if (!isRuntimeObject)
            {
                long assemblyId = (long)reader.ReadUInt32();
                metadata.Type = GetDeserializationType(assemblyId, className);
            }
            else
            {
                metadata.Type = Type.GetType(className, true);
            }

            metadata.MemberTypes = types;
            metadata.MemberNames = names;
            metadata.FieldCount  = names.Length;

            // Now check if this objects needs a SerializationInfo struct for deserialziation.
            // SerializationInfo is needed if the object has to be deserialized using
            // a serialization surrogate, or if it implements ISerializable.

            if (_surrogateSelector != null)
            {
                // check if the surrogate selector handles objects of the given type.
                ISurrogateSelector      selector;
                ISerializationSurrogate surrogate = _surrogateSelector.GetSurrogate(metadata.Type, _context, out selector);
                metadata.NeedsSerializationInfo = (surrogate != null);
            }

            if (!metadata.NeedsSerializationInfo)
            {
                // Check if the object is marked with the Serializable attribute

                if (!metadata.Type.IsSerializable)
                {
                    throw new SerializationException("Serializable objects must be marked with the Serializable attribute");
                }

                metadata.NeedsSerializationInfo = typeof(ISerializable).IsAssignableFrom(metadata.Type);
                if (!metadata.NeedsSerializationInfo)
                {
                    metadata.MemberInfos = new MemberInfo [fieldCount];
                    for (int n = 0; n < fieldCount; n++)
                    {
                        FieldInfo field      = null;
                        string    memberName = names[n];

                        int i = memberName.IndexOf('+');
                        if (i != -1)
                        {
                            string baseTypeName = names[n].Substring(0, i);
                            memberName = names[n].Substring(i + 1);
                            Type t = metadata.Type.BaseType;
                            while (t != null)
                            {
                                if (t.Name == baseTypeName)
                                {
                                    field = t.GetField(memberName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                                    break;
                                }
                                else
                                {
                                    t = t.BaseType;
                                }
                            }
                        }
                        else
                        {
                            field = metadata.Type.GetField(memberName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                        }

                        if (field != null)
                        {
                            metadata.MemberInfos [n] = field;
                        }
#if ONLY_1_1
                        else
                        {
                            throw new SerializationException("Field \"" + names[n] + "\" not found in class " + metadata.Type.FullName);
                        }
#endif

                        if (!hasTypeInfo)
                        {
                            types [n] = field.FieldType;
                        }
                    }
                    metadata.MemberNames = null; // Info now in MemberInfos
                }
            }

            // Registers the type's metadata so it can be reused later if
            // a RefTypeObject element is found

            if (!_typeMetadataCache.ContainsKey(metadata.Type))
            {
                _typeMetadataCache [metadata.Type] = metadata;
            }

            return(metadata);
        }
Example #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;
                }
            }
        }
Example #11
0
 public MetadataReference(TypeMetadata metadata, long id)
 {
     Metadata = metadata;
     ObjectID = id;
 }
Example #12
0
 public MetadataReference(TypeMetadata metadata, long id)
 {
     this.Metadata = metadata;
     this.ObjectID = id;
 }
Example #13
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;
                }
            }
        }