private void SerializeReflection(object o) { Type type = o.GetType(); // Serialize fields. foreach (FieldInfo field in BinarySerializationReflectionUtils.ReflectFields(type)) { object fieldValue = field.GetValue(o); this.Serialize(fieldValue, field.FieldType); } // Serialize properties. foreach (PropertyInfo property in BinarySerializationReflectionUtils.ReflectProperties(type)) { object propertyValue = property.GetGetMethod().Invoke(o, null); this.Serialize(propertyValue, property.PropertyType); } }
private object DeserializeReflection(Type type) { // Create object instance. object o = Activator.CreateInstance(type); // Deserialize fields. foreach (FieldInfo field in BinarySerializationReflectionUtils.ReflectFields(type)) { try { object fieldValue = this.Deserialize(field.FieldType); field.SetValue(o, fieldValue); } catch (Exception e) { throw new SerializationException( string.Format("Unable to deserialize field {0}.{1}.", type.FullName, field.Name), e); } } // Deserialize properties. foreach (PropertyInfo property in BinarySerializationReflectionUtils.ReflectProperties(type)) { try { object propertyValue = this.Deserialize(property.PropertyType); property.SetValue(o, propertyValue, null); } catch (Exception e) { throw new SerializationException( string.Format("Unable to deserialize property {0}.{1}.", type.FullName, property.Name), e); } } return(o); }