从接口强制执行可序列化而不强制类在C#中自定义序列化

时间:2022-07-15 19:49:48

I have an interface that defines some methods I would like certain classes to implement.

我有一个接口,定义了一些我希望某些类实现的方法。

public interface IMyInterface
{
    MethodA;
    MethodB;
}

Additionally I would like all classes implementing this interface to be serializable. If I change the interface definition to implement ISerializable as below...:

另外,我希望实现此接口的所有类都可以序列化。如果我更改接口定义以实现ISerializable如下...:

public interface IMyInterface : ISerializable
{
    MethodA;
    MethodB;
}

...all classes must now explicitly implement serialization as far as I am aware, since if you implement ISerializable you must implement the GetObjectData member (and the necessary constructor to deserialize).

...据我所知,所有类现在必须显式实现序列化,因为如果实现ISerializable,则必须实现GetObjectData成员(以及反序列化所需的构造函数)。

How can I insist classes using my interface be serializable, but without forcing them to custom implement serialization?

如何坚持使用我的界面的类是可序列化的,但不强制他们自定义实现序列化?

Thanks, Will

5 个解决方案

#1


10  

There does not seem to be a way to do this, but I wish there were.

似乎没有办法做到这一点,但我希望有。

Note two things though:

请注意两件事:

  • The Serializable attribute can not be inherited from a base class, even if the base class is marked as abstract.

    即使基类标记为抽象,也不能从基类继承Serializable属性。

  • You don't technically need the Serializable attribute, IF you are using an XmlSerializer because it does not make use of object graphs.

    从技术上讲,您不需要Serializable属性,如果您使用的是XmlSerializer,因为它不使用对象图。

#2


1  

If you want to use the default serialization then you need to add the SerializableAttribute . One alternative would be to use an abstract class instead of an interface, then add the SerializableAttribute to the abstract class. If you're implementing your own custom serialization then you want to implement ISerializable, otherwise stick with SerializableAttribute .

如果要使用默认序列化,则需要添加SerializableAttribute。一种替代方法是使用抽象类而不是接口,然后将SerializableAttribute添加到抽象类。如果您正在实现自己的自定义序列化,那么您希望实现ISerializable,否则坚持使用SerializableAttribute。

#3


1  

Write a class that implements ISerializationSurrogate and use that to serialize classes that aren't serializable or where you're not sure of the serialization implementation.

编写一个实现ISerializationSurrogate的类,并使用它来序列化不可序列化的类或者您不确定序列化实现的类。

#4


0  

You could write a custom FxCop rule and validate check-ins against it.

您可以编写自定义FxCop规则并验证它的签入。

#5


0  

Thanks for the replies. It would be nice to be able to force classes derived from an interface to implement serialization without this then forcing them to custom serialize but it doesn't seem to be possible.

谢谢你的回复。如果能够强制从接口派生的类来实现序列化而不使用它然后强制它们进行自定义序列化会很好,但似乎不可能。

#1


10  

There does not seem to be a way to do this, but I wish there were.

似乎没有办法做到这一点,但我希望有。

Note two things though:

请注意两件事:

  • The Serializable attribute can not be inherited from a base class, even if the base class is marked as abstract.

    即使基类标记为抽象,也不能从基类继承Serializable属性。

  • You don't technically need the Serializable attribute, IF you are using an XmlSerializer because it does not make use of object graphs.

    从技术上讲,您不需要Serializable属性,如果您使用的是XmlSerializer,因为它不使用对象图。

#2


1  

If you want to use the default serialization then you need to add the SerializableAttribute . One alternative would be to use an abstract class instead of an interface, then add the SerializableAttribute to the abstract class. If you're implementing your own custom serialization then you want to implement ISerializable, otherwise stick with SerializableAttribute .

如果要使用默认序列化,则需要添加SerializableAttribute。一种替代方法是使用抽象类而不是接口,然后将SerializableAttribute添加到抽象类。如果您正在实现自己的自定义序列化,那么您希望实现ISerializable,否则坚持使用SerializableAttribute。

#3


1  

Write a class that implements ISerializationSurrogate and use that to serialize classes that aren't serializable or where you're not sure of the serialization implementation.

编写一个实现ISerializationSurrogate的类,并使用它来序列化不可序列化的类或者您不确定序列化实现的类。

#4


0  

You could write a custom FxCop rule and validate check-ins against it.

您可以编写自定义FxCop规则并验证它的签入。

#5


0  

Thanks for the replies. It would be nice to be able to force classes derived from an interface to implement serialization without this then forcing them to custom serialize but it doesn't seem to be possible.

谢谢你的回复。如果能够强制从接口派生的类来实现序列化而不使用它然后强制它们进行自定义序列化会很好,但似乎不可能。