How to enforce xml serialization through an interface?
Currently i have some interfaces (stripped down for here):
public interface IJobGroup
{
string Name { get; }
IEnumerable<IJobItem> Jobs { get; }
}
public interface IJobItem
{
string Name { get; }
void Start();
event EventHandler Finished;
}
Also i made some implementations of these interfaces. But now i'd like to serialize my JobGroup
. So far i already made some tries with DataContractSerializer and XMLSerializer and added all the needed attributes to my implementations of the above interfaces.
So far, so good. But i'd like to get two things solved with my needed solution:
- Enforce everyone who implements my interface to make his class serializable to xml.
- If i serialize a
IJobGroup
, i'd like to see everything in the resulting xml file in clear text, not as Base64 coded data. In that case someone is able to change the xml file by hand (if he knows what he's doing).
Some ideas, which doesn't work very well:
ISerializable
This doesn't really enforce to serialize an object. It is rather used to implement a custom serializer instead of using the default usage of attributes.
property within interfaces that encapsulated the serialization (e.g.
byte[] State
)I could add another byte array property State to the interface, which will be get and 开发者_运维问答set the serialized data. Maybe this would have the advantage that every item could implement its very own process of how it will serialize something. But the drawback would be that the big xml file contains the byte[] as Base64 coded chunk of data, which can't be read by human easily.
So, maybe someone has an idea or could point me into the right direction.
How about using abstract classes? I don't think you can enforce behavior with interfaces... interfaces are more contract-oriented than anything else...
The default XML Serialization doesn't support interfaces by default, but you might be able to derive from XmlSerializer and build something using that framework?
精彩评论