简体   繁体   English

C#序列化问题

[英]C# Serialization Issue

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 :) 我在使用C#进行二进制序列化时遇到了一些麻烦...我应该补充一点,我是C#的相对菜鸟,所以请用简单的术语进行解释:)

Anyway, I have this class (truncated for clarity): 无论如何,我有这个课(为清楚起见被截断):

[Serializable]
public class Player : Ship {

And Ship looks like this (again, truncated): Ship看起来像这样(再次被截断):

[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. 因此,如您所见,我希望序列化Player类。 However, when I try this, I get this error: 但是,当我尝试此操作时,出现以下错误:

An unhandled exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.dll mscorlib.dll中发生了类型为'System.Runtime.Serialization.SerializationException'的未处理异常

Additional information: Type 'MoXNA.ParticleMachines.MinSpeedRippleTrail' in Assembly 'SpaceshipInABox, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable. 附加信息:在装配'SpaceshipInABox,版本= 1.0.0.0,区域性=中性,公共密钥令牌=空'中,类型'MoXNA.ParticleMachines.MinSpeedRippleTrail'未标记为可序列化。

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). 奇怪的是,我将rippleTrail (这是MinSpeedRippleTrail对象)标记为[NonSerialized] ...我不想对其进行序列化(这只是视觉效果)。

I used "Find all references" and that's the only MinSpeedRippleTrail object in the entire program, so what the hell? 我使用了“查找所有引用”,这是整个程序中唯一的MinSpeedRippleTrail对象,那又如何呢?

Thanks. 谢谢。

The usual problem here is an event. 通常的问题是一个事件。 BinaryFormatter includes events, which many people don't expect. BinaryFormatter包含许多人不期望的事件。 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: 但是,我也鼓励您:

  • don't use public fields; 不要使用公共领域; use properties 使用属性
  • don't use BinaryFormatter - it bites most people eventually; 不要使用BinaryFormatter-最终会咬住大多数人; and bites hard 硬咬

If you want a binary serializer that works with XNA, consider protobuf-net; 如果您想使用与XNA兼容的二进制序列化程序,请考虑使用protobuf-net;否则,请转至http://www.protobuf.com/cn it is faster, has smaller output, adapts to change more gracefully ... and is free. 它速度更快,输出更小,可以更顺畅地进行更改...并且是免费的。 Caveat: I wrote it. 警告:我写的。

If you are using XML serialization you can use XmlIgnoreAttribute , otherwise change rippleTrail member to a method. 如果使用的是XML序列化,则可以使用XmlIgnoreAttribute ,否则可以将涟漪追踪成员更改为方法。 for example: GetRippleTrail() 例如:GetRippleTrail()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM