SerializedAtttribute 与 ISerialized

发布于 2024-10-16 21:09:40 字数 384 浏览 1 评论 0原文

我有扩展方法

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

人生百味 2024-10-23 21:09:40

有两种方法可以使类型可序列化。

  • 实现 ISerialized (较少使用)
  • 应用 [Serializable] 属性 (更多使用且更容易)

在您的情况下,您只考虑第一个。

要解决这个问题,只需让序列化器处理错误报告即可。对于任何不可序列化的对象,它将引发错误。

There are two ways you can make a type serializable.

  • Implement ISerializable (Less used)
  • Apply [Serializable] attribute (More used and easier)

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.

沦落红尘 2024-10-23 21:09:40

问题在于分配属性 (Serialized) 与实现接口 (ISerialized) 没有任何共同之处。

The problem is that assigning an attribute (Serializable) has nothing in common with implementing an interface (ISerializable).

无人问我粥可暖 2024-10-23 21:09:40

只是想我会对这个答案多一点。 [Serialized] 与 ISerialized 曾让我困惑了一段时间,但我终于弄清楚了。

当您希望一个类可序列化时,您可以应用 [Serializable] 属性。这就是您所需要的(今天也是如此)。然而,有时您需要对序列化过程进行更精细的控制(也许序列化对象的接收者有一些严格的要求,而您的对象不太关心)。在 .NET 2.0 之前的时代,为了处理对象序列化方式的任何自定义,除了应用 [Serialized] 属性之外,您还实现了 ISerialized 接口。

ISerialized 接口有一个单一的功能要求:

void GetObjectData(SerializationInfo info,StreamingContext context)

在这个功能中,您序列化了您的内容:

GetObjectData(SerializationInfo info,StreamingContext context)
{
   // example of some type of customization of the serialization process, giving
   // your serialized class member a custom name
   info.AddValue("GiveMyDataMemberCustomNameInOutput", myDataMember);
   ...

随着一些附加 [Serializable] 属性的出现 (.NET 2.0),您不需要实现 ISerialized.GetObjectData() 来自定义 < em>如何某物被序列化。

那么如何以新的方式处理时髦的序列化呢?那么,[Serialized] 有一些合作伙伴,特别是 [OnSerializing] 和 [OnDeserialized]。如果您需要执行以前使用 ISerialized.GetObjectData() 执行的时髦序列化/反序列化,您现在可以在事件中处理它。

我想指出两件事:

  1. 没有太多理由(IMO,但可能是边缘情况)使用/实现 ISerialized 接口。只需使用较新的 [Serializable] 属性
  2. 如果您觉得需要 ISerializable,那么请记住,您还必须将 [Serializable] 属性应用到您的类。

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:

void GetObjectData(SerializationInfo info,StreamingContext context)

In this function you serialized your content:

GetObjectData(SerializationInfo info,StreamingContext context)
{
   // example of some type of customization of the serialization process, giving
   // your serialized class member a custom name
   info.AddValue("GiveMyDataMemberCustomNameInOutput", myDataMember);
   ...

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:

  1. There's not much of a reason (IMO, but could be edge cases) to use/implement the ISerializable interface. Just use the newer [Serializable] attributes
  2. If you feel you need ISerializable, then remember you must also apply the [Serializable] attribute to your class.
凉宸 2024-10-23 21:09:40

您必须将接口 ISerialized 添加到您的类中

public class YourClass : ISerializable

You must add the interface ISerializable to your classes

public class YourClass : ISerializable
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文