简体   繁体   English

不使用[Serializable]时[NonSerialized]的好处

[英]Benefits of [NonSerialized] when [Serializable] is not used

I'm looking through some existing code in a project I'm working on, and I found a class that is implemented as: 我正在查看我正在处理的项目中的一些现有代码,我找到了一个实现为:

public class ThingOne
{
    private int A;

    private int B;

    [NonSerialized]
    private System.Timers.Timer timer1;
}

Shouldn't it look more like this? 它不应该看起来更像这样吗?

[Serializable]
public class ThingOne
{
    private int A;

    private int B;

    [NonSerialized]
    private System.Timers.Timer timer1;
}

Or is there some additional benefit to adding [NonSerialized] even when the class itself is not Serializable? 或者,即使类本身不是Serializable,添加[NonSerialized]还有一些额外的好处吗?

Or is there some additional benefit to adding [NonSerialized] even when the class itself is not Serializable? 或者,即使类本身不是Serializable,添加[NonSerialized]还有一些额外的好处吗?

The class isn't sealed, so another class could inherit from that object. 该类未密封,因此另一个类可以从该对象继承。 That class could be marked as Serializable, and then the NotSerializable attribute would come into play. 该类可以标记为Serializable,然后NotSerializable属性将起作用。 (although as pointed out not for private members). (虽然没有指出私人会员)。

Remember you can check attributes by reflection too. 请记住,您也可以通过反射检查属性。 It may not be used by the runtime to check what should and should not be serialized, it could be used as a marker for something else in the program dealing with some sort of custom serialization (I'm not saying this is a good idea in the least). 运行时可能不会使用它来检查应该和不应该序列化的内容,它可以用作程序中其他东西的标记来处理某种自定义序列化(我不是说这是个好主意)至少)。

NonSerialized will have no effect when Serializable is not used. 未使用Serializable时,NonSerialized将不起作用。 By default, classes and their members are non-serializable. 默认情况下,类及其成员是不可序列化的。

The only advantage of declaring something NonSerialized when the class isn't serialized is under the circumstances that the class is inherited by a Serialized object, and then the inherited member will be non-serializable. 当类未被序列化时声明NonSerialized的唯一优点是在类被Serialized对象继承的情况下,然后继承的成员将是不可序列化的。

From MSDN : 来自MSDN

'NonSerialized' attribute will not affect this member because its containing class is not exposed as 'Serializable'. 'NonSerialized'属性不会影响此成员,因为其包含的类未公开为'Serializable'。

By default, classes and their members are non-serializable. 默认情况下,类及其成员是不可序列化的。 The NonSerializedAttribute attribute is only needed if a member of a serializable class should not be serialized. 仅当序列化类的成员不应序列化时,才需要NonSerializedAttribute属性。

I can think of two reasons: 我可以想到两个原因:

  1. It could be vital that the field isn't serialised. 该领域不是序列化的至关重要。 Hence if in the future the class is made serialisable, this won't introduce a bug, inefficiency or security issue, because without it marking the class serialisable will also do so for the field. 因此,如果将来该类具有可序列性,则不会引入错误,低效或安全问题,因为如果没有它标记类serialisable也将为该字段执行此操作。

  2. They could be doing some sort of custom use of the attribute 他们可以对属性进行某种自定义使用

In case 2 it'll be clear from elsewhere in the code that this is what's happening. 在案例2中,从代码的其他地方可以清楚地看到这就是正在发生的事情。 Number 1 is good practice though. 但是,1号是很好的做法。

Case 1 is good practice, it can be worth balancing YAGNI ("You Aren't Gonna Need It" - not doing work "in case it's needed later") with considering "okay, but if I do need it later, it'll be a disaster if someone misses that this field is an exception. 案例1是一个很好的做法,值得平衡YAGNI(“你不会需要它” - 不做工作“以防以后需要”)考虑“好吧,但如果我以后需要它,它会'如果有人错过了这个字段是一个例外,那将是一场灾难。

So, while it has no effect here, it is definitely a good practice for scenarios where it begins to have an effect. 因此,虽然它在这里没有任何影响,但对于开始产生影响的场景来说绝对是一个很好的做法。

Edit: Another possibility is that it is cruft from a previous version where it was indeed serialisable or the author was in two minds at the time and it was never entirely "finished" (is working code ever entirely finished?). 编辑:另一种可能性是,它是从以前的版本,它确实是可序列化的或者作者当时有两个想法并且从未完全“完成”(工作代码是否完全完成?)。 Just because something is in code, doesn't mean it was meant to be that way. 仅仅因为某些东西在代码中,并不意味着它就是那种方式。 Still, if it's really important that something not be serialised, I still say it's good practice to mark this for the reason given above. 尽管如此,如果某些事情没有被序列化是非常重要的,我仍然说这是出于上述原因而标记这一点的好习惯。

MSDN SerializeAttribute states that "Apply the SerializableAttribute attribute to a type to indicate that instances of this type can be serialized. " This implies that without it, the class cannot be serialized. MSDN SerializeAttribute声明“将SerializableAttribute属性应用于某个类型以指示此类型的实例可以序列化。”这意味着没有它,该类无法序列化。 I believe I have attempted this and the serialize will throw an exception if it is attempted on a NonSerializable Type. 我相信我已经尝试过这个,如果尝试在NonSerializable类型上,序列化将抛出异常。

I agree with Greg, MSDN states it in a similar manner, citing references is a good idea.. 我同意Greg,MSDN以类似的方式说明它,引用引用是个好主意。

"By default, classes and their members are non-serializable. The NonSerializedAttribute attribute is only needed if a member of a serializable class should not be serialized." “默认情况下,类及其成员是不可序列化的。仅当序列化类的成员不应序列化时,才需要NonSerializedAttribute属性。”

http://msdn.microsoft.com/en-us/library/dwys85sk(VS.80).aspx http://msdn.microsoft.com/en-us/library/dwys85sk(VS.80).aspx

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

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