0

So, I've been looking at all kinds of examples of a WCF class that is to be serialized. I'd like to define all my DataContracts and DataMembers in interfaces and then allow implementation that suits the environment. Is this possible, and if so, are there drawbacks to this approach?

[DataContract]
public class Contact
{
    [DataMember]
    public int Roll { get; set; }

    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public string Address { get; set; }

    [DataMember]
    public int Age { get; set; }
}

Can instead do this:

[DataContract]
public interface IContact
{
    [DataMember]
    int Roll { get; set; }

    [DataMember]
    string Name { get; set; }

    [DataMember]
    string Address { get; set; }

    [DataMember]
    int Age { get; set; }
}

public class Contact :IContact
{
    public int Roll { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public int Age { get; set; }
}
Jeff Reddy
  • 5,551
  • 9
  • 55
  • 88
  • 2
    You don't _need_ to annotate with DataContract/DataMember, and yes there's a drawback: those attributes aren't inherited, so they're ignored. Why do you want to declare them on your interfaces? – CodeCaster May 20 '16 at 12:23
  • 1
    Don't locate attributes on interface members: http://stackoverflow.com/questions/12106566/attribute-on-interface-members-does-not-work – Ilya Chumakov May 20 '16 at 14:15

0 Answers0