SerializedAtttribute 与 ISerialized
我有扩展方法
public static T DeepClone<T>(this T source) where T : ISerializable
{
..
}
当我添加“where T : ISerialized”时,我在使用具有 [Serializable] 属性的 DeepClone() 的所有类上都会出现错误。
- 问题是什么?
编辑:
- 我知道我的类应该实现 ISerialized 才能工作。但是使用 ISerialized 与 [Serialized] 时序列化输出有什么区别吗?
I have extension method
public static T DeepClone<T>(this T source) where T : ISerializable
{
..
}
When I added "where T : ISerializable" I get error on all classes that uses DeepClone() having [Serializable] attribute.
- What is the problem?
EDIT:
- I know that my classes should implement ISerializable to work. But is there any difference in serialized output when using ISerializable vs [Serializable]?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有两种方法可以使类型可序列化。
在您的情况下,您只考虑第一个。
要解决这个问题,只需让序列化器处理错误报告即可。对于任何不可序列化的对象,它将引发错误。
There are two ways you can make a type serializable.
In your case, you only consider the first one.
To solve this, just let the serializer handle error reporting. It will throw error for any object that is not serializable.
问题在于分配属性 (
Serialized
) 与实现接口 (ISerialized
) 没有任何共同之处。The problem is that assigning an attribute (
Serializable
) has nothing in common with implementing an interface (ISerializable
).只是想我会对这个答案多一点。 [Serialized] 与 ISerialized 曾让我困惑了一段时间,但我终于弄清楚了。
当您希望一个类可序列化时,您可以应用 [Serializable] 属性。这就是您所需要的(今天也是如此)。然而,有时您需要对序列化过程进行更精细的控制(也许序列化对象的接收者有一些严格的要求,而您的对象不太关心)。在 .NET 2.0 之前的时代,为了处理对象序列化方式的任何自定义,除了应用 [Serialized] 属性之外,您还实现了 ISerialized 接口。
ISerialized 接口有一个单一的功能要求:
在这个功能中,您序列化了您的内容:
随着一些附加 [Serializable] 属性的出现 (.NET 2.0),您不需要实现 ISerialized.GetObjectData() 来自定义 < em>如何某物被序列化。
那么如何以新的方式处理时髦的序列化呢?那么,[Serialized] 有一些合作伙伴,特别是 [OnSerializing] 和 [OnDeserialized]。如果您需要执行以前使用 ISerialized.GetObjectData() 执行的时髦序列化/反序列化,您现在可以在事件中处理它。
我想指出两件事:
Just thought I would a little bit more to this answer. The [Serializable] vs ISerializable was something that confused me for a while and I finally got it straightened out.
When you want a class to be serializable, you apply the [Serializable] attribute. That's all you needed (and that is true today). However, there are times where you need finer control of the serialization process (maybe the receiver of the serialized object has some strict requirements that your object is less concerned with). To deal with any customization of how the object gets serialized in the pre-.NET 2.0 days, you implemented the ISerializable interface in addition to applying the [Serializable] attribute.
The ISerializable interface has a single function requirement:
In this function you serialized your content:
With the advent (.NET 2.0) of some additional [Serializable] attributes, you don't need to implement ISerializable.GetObjectData() in order to customize how something was serialized.
So how do you handle funky serialization the newer way? Well [Serializable] has some partners, notably [OnSerializing] and [OnDeserialized]. If you need to do the funky serialization/deserialization that you used to do with ISerializable.GetObjectData() you now handle it in events.
Two things I want to point out:
您必须将接口 ISerialized 添加到您的类中
You must add the interface ISerializable to your classes