void System.Runtime.Serialization.ISerializationSurrogate.GetObjectData(object obj, System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
{
Type objectType = obj.GetType();
foreach (FieldInfo member in objectType.FindMembers(MemberTypes.Field, BindingFlags.Instance|BindingFlags.NonPublic|BindingFlags.Public, null, null))
{
if (member.IsDefined(typeof(NonSerializedAttribute), false))
{
continue;
}
info.AddValue(member.Name, member.GetValue(obj));
}
/* FIX: Rutger M. Dijkstra ([email protected])
* Add the private fields of ancestor classes
*
* Note 1: The potentially ambiguous naming strategy below is the same
* as used by the standard (non-surrogate) binary serialization.
*
* Note 2: Even so, we are not compatible with the standard serialization
* since the latter emits inherited *protected* fields multiple times.
* Consequently, snapshots created with auto version-migration 'on'
* need not correctly deserialize with version-migration 'off'
*
* Afterthought:
* Maybe, 'auto version-migration' should affect only the process of
* DEserialization, not the serialization as well. On the other hand,
* we might not want tie ourselves too tightly to the details of the
* standard implementation.
*/
for (objectType = objectType.BaseType; objectType != null; objectType = objectType.BaseType)
{
foreach (FieldInfo member in objectType.FindMembers(MemberTypes.Field, BindingFlags.Instance|BindingFlags.NonPublic|BindingFlags.DeclaredOnly, null, null))
{
if (member.IsPrivate && !member.IsNotSerialized)
{
info.AddValue(objectType.Name + "+" + member.Name, member.GetValue(obj));
}
}
}
}