I've got a problem with serializing an object graph using a surrogate. I get this error {"A fixup on an object implementing ISerializable or having a surrogate was discovered for an object which does not have a SerializationInfo available."}
In it's simplest form I have an object with a child object...
[Serializable]
public class _MyObject
{
internal protected _MyChildObject[] items;
public _MyObject()
{
}
}
where _MyChildObject is implemented as:
[Serializable]
public class _MyChildObject
{
internal protected decimal[] values;
public _MyChildObject(decimal amount)
{
this.values = new decimal[] { amount };
}
}
These objects serialize and deserialize perfectly with standard (de)serialization code:
_MyObject _myObject = new _MyObject();
_myObject.items = new _MyChildObject[] { new _MyChildObject(36m) };
Stream s = File.Open("temp.dat", FileMode.Create);
BinaryFormatter b = new BinaryFormatter();
b.Serialize(s, _myObject);
s.Close();
When I introduce a SerializationSurrogate I get the above error. My Surrogate implements GetObjectData like this:
public static void GetObjectData(object obj, SerializationInfo info, StreamingContext context)
{
Type objectType = obj.GetType();
foreach (FieldInfo member in objectType.FindMembers(MemberTypes.Field, BindingFlags.Instance|BindingFlags.NonPublic|BindingFlags.Public|BindingFlags.FlattenHierarchy, null, null))
{
if (member.IsDefined(typeof(NonSerializedAttribute), false))
continue;
object test = member.GetValue(obj);
info.AddValue(member.Name, test);
}
}
If I remove the array inside _MyObject and replace with a single _MyChildObject then serialization/deserialization works fine with the surrogate.
What's up here Can anyone point me towards a fix for this
Carl