/// <summary> /// Populates the object using the information in the <see cref="T:System.Runtime.Serialization.SerializationInfo"></see>. /// </summary> /// <param name="obj">The object to populate.</param> /// <param name="info">The information to populate the object.</param> /// <param name="context">The source from which the object is deserialized.</param> /// <param name="selector">The surrogate selector where the search for a compatible surrogate begins.</param> /// <returns>The populated deserialized object.</returns> /// <exception cref="T:System.ArgumentNullException">The SerializationInfo info cannot be null.</exception> /// <exception cref="T:System.SerializationException">Array types are not supported.</exception> /// /// <exception cref="T:System.Security.SecurityException">The caller does not have the required permission.</exception> public object SetObjectData(object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector) { if (null == info) { throw new ArgumentNullException("info", "Serialization info cannot be null."); } //Assembly assembly = Assembly.Load(info.AssemblyName); //Type objectType = assembly.GetType(info.FullTypeName); string assemblyQualifiedTypeName = string.Format("{0}, {1}", info.FullTypeName, info.AssemblyName); Type objectType = new TypeParser().Resolve(assemblyQualifiedTypeName); if (objectType.IsArray) { throw new SerializationException(String.Format("The ObjectSerializationSurrogate does not support Array types. Type '{0}' in Assembly '{1}' is an Array type.", objectType.FullName, objectType.Assembly.FullName)); } if (null == obj) { obj = FormatterServices.GetUninitializedObject(objectType); } List <FieldInfo> fields = new List <FieldInfo>(); List <object> values = new List <object>(); MemberInfo[] serializableMembers = FormatterServices.GetSerializableMembers(objectType, context); foreach (SerializationEntry entry in info) { FieldInfo field = ObjectSerializationSurrogate.getField(serializableMembers, entry.Name); fields.Add(field); values.Add(entry.Value); } FormatterServices.PopulateObjectMembers(obj, fields.ToArray(), values.ToArray()); return(obj); }
private void initializeSystemSurrogateSelector(StreamingContext context, bool enableEventSerialization) { SurrogateSelector surrogateSelector = new SurrogateSelector(); surrogateSelector.AddSurrogate(typeof(DateTime), context, new DateTimeSerializationSurrogate()); surrogateSelector.AddSurrogate(typeof(ISerializable), context, new SerializableSerializationSurrogate()); ObjectSerializationSurrogate objectSurrogate = new ObjectSerializationSurrogate(); objectSurrogate.EnableEventSerialization = enableEventSerialization; surrogateSelector.AddSurrogate(typeof(object), context, objectSurrogate); this.systemSurrogateSelector = surrogateSelector; }