private void SerializeObject(object currentObject, long currentObjectId) { bool needsSerializationInfo = false; ISurrogateSelector selector; ISerializationSurrogate surrogate = null; if (_surrogateSelector != null) { surrogate = _surrogateSelector.GetSurrogate( currentObject.GetType(), _context, out selector); } if (currentObject is ISerializable || surrogate != null) { needsSerializationInfo = true; } #if NET_2_0 && !TARGET_JVM _manager.RegisterObject(currentObject); #endif if (needsSerializationInfo) { SerializeISerializableObject(currentObject, currentObjectId, surrogate); } else { if (!currentObject.GetType().IsSerializable) { throw new SerializationException(String.Format("Type {0} in assembly {1} is not marked as serializable.", currentObject.GetType(), currentObject.GetType().Assembly.FullName)); } SerializeSimpleObject(currentObject, currentObjectId); } }
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; } } }