Oliver
Oliver

Reputation: 45081

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:

  1. Enforce everyone who implements my interface to make his class serializable to xml.
  2. 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:

So, maybe someone has an idea or could point me into the right direction.

Upvotes: 1

Views: 270

Answers (2)

code4life
code4life

Reputation: 15794

How about using abstract classes? I don't think you can enforce behavior with interfaces... interfaces are more contract-oriented than anything else...

Upvotes: 2

Rowland Shaw
Rowland Shaw

Reputation: 38130

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?

Upvotes: 0

Related Questions