Reputation: 7963
I'm having some trouble with binary serialization in C#... I should add that I'm a relative noob with C#, so please explain in simple terms :)
Anyway, I have this class (truncated for clarity):
[Serializable]
public class Player : Ship {
And Ship looks like this (again, truncated):
[Serializable]
public class Ship : TiledActor {
public float MaxUserTurnPerSecond;
public float RemainingShieldStrength;
public float MaxShieldStrength;
public float ShieldRechargeRate;
public float ShieldRechargeDelay;
public Color ShieldColor;
public float ThrusterAccelerationScale;
public Color RippleTrailColor;
public float MinimumRippleSpeed;
public float MaxEnergy;
public float CurrentEnergy;
public float EnergyRechargeRate;
public float MaxSecondaryAmmoCapacity;
public int CurrentSecondaryAmmoCount;
public Weapon PrimaryWeapon;
public Weapon SecondaryWeapon;
protected ShieldActor ShieldTex;
[NonSerialized]
public MoXNA.ParticleMachines.PM_Explosion_Particle_Count ExplosionSize;
[NonSerialized]
protected MoXNA.ParticleMachines.MinSpeedRippleTrail rippleTrail;
/* ... Truncated here */
So, as you can see, I wish to serialize the Player class. However, when I try this, I get this error:
An unhandled exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.dll
Additional information: Type 'MoXNA.ParticleMachines.MinSpeedRippleTrail' in Assembly 'SpaceshipInABox, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.
The odd thing here is that I marked rippleTrail (which is the MinSpeedRippleTrail object) as [NonSerialized]... I don't want to serialize it (it's just a visual effect).
I used "Find all references" and that's the only MinSpeedRippleTrail object in the entire program, so what the hell?
Thanks.
Upvotes: 2
Views: 2183
Reputation: 1064204
The usual problem here is an event. BinaryFormatter includes events, which many people don't expect. The event field must be marked, for example:
[field:NonSerialized]
public event EventHandler Foo;
It is painfully easy to get an event subscription that you didn't anticipate break the serializer (a capture class, perhaps).
However, I also encourage you:
If you want a binary serializer that works with XNA, consider protobuf-net; it is faster, has smaller output, adapts to change more gracefully ... and is free. Caveat: I wrote it.
Upvotes: 4
Reputation: 9144
If you are using XML serialization you can use XmlIgnoreAttribute, otherwise change rippleTrail member to a method. for example: GetRippleTrail()
Upvotes: 0