Following your advice I wrote some tests. It ends up that when you put the classes in a list of type baseclass, the serializer uses the attributes of the base class/interface instead. That's a little weird. I don't believe the standard .Net serializers work that way. This test fails on the very last line:
interface ITest {
string One { get; set; }
string Two { get; set; }
}
class Test1: ITest
{
public string One { get; set; }
[IgnoreDataMember]
public string Two { get; set; }
}
[DataContract]
class Test2 : ITest
{
[DataMember]
public string One { get; set; }
public string Two { get; set; }
}
[Test]
public void ContractAttribsWork()
{
var c1 = new Test1 { One = "1", Two = "2" };
var c2 = new Test2 { One = "1", Two = "2" };
var c1s = JsonSerializer.SerializeToString(c1);
Assert.IsTrue(c1s.Contains("One"));
Assert.IsFalse(c1s.Contains("Two"));
var c2s = JsonSerializer.SerializeToString(c2);
Assert.IsTrue(c2s.Contains("One"));
Assert.IsFalse(c2s.Contains("Two"));
var list = new List<ITest> {c1, c2};
var c3s = JsonSerializer.SerializeToString(list);
Assert.IsTrue(c3s.Contains("One"));
Assert.IsFalse(c3s.Contains("Two"));
}