为什么接口不是 [Serializable]?

编程入门 行业动态 更新时间:2024-10-21 06:21:48
本文介绍了为什么接口不是 [Serializable]?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我认为将该属性添加到接口将有助于确保您不会创建使用该接口的类并忘记使它们可序列化.

I would think that adding that attribute to an interface would be helpful make sure you do not create classes that use the interface and forget to make them serializable.

这可能是一个非常基本的问题,但我想请教专家.

This could be a very fundamental question, but I wanted to ask the experts.

推荐答案

接口定义了一个契约并且没有自己的任何状态.

Interfaces define a contract and do not have any state of their own.

序列化是关于在对象模型中保存和加载状态.

Serialization is about saving and loading state into and out of an object model.

序列化没有状态的东西没什么意义.

Not much point to serializing something that holds no state.

为了回答强制接口的实现为 Serializable 的实际问题 - 这就是 ISerializable 接口存在.

To answer the practical question of forcing an implementation of an interface to be Serializable - this is why the ISerializable interface exists.

在 .NET 中,您可以声明一个应该实现其他接口的接口:

In .NET you can declare an interface that should implement other interfaces:

interface MustBeSerializable : ISerializable {}

在此处查看更多信息.

更多推荐

为什么接口不是 [Serializable]?

本文发布于:2023-06-07 15:18:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/568511.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:接口   Serializable

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!