3

I have a class with countless members.

I want to use DataContract serialization/deserialization and so the class has the [DataContract] attribute. If I put the [DataMember] attribute that works otherwise it doesn't.

Am I to put [DataMember] on all of them? Isn't there a way to automatically set ALL of them as datamembers?

Thanks

--EDIT--

The solution in Set DataContract and DataMember Without All the Attributes is not working.

public Test()
{
  if (false)
  {

    MyClass theclass = new MyClass();

    var serializer = new DataContractSerializer(typeof(MyClass));

    using (Stream fileStream = File.Open("aaa.bin", FileMode.Create))
    {
      XmlDictionaryWriter binaryDictionaryWriter = XmlDictionaryWriter.CreateBinaryWriter(fileStream);
      serializer.WriteObject(binaryDictionaryWriter, theclass);
      binaryDictionaryWriter.Flush();
    }
  }
  else
  {
    MyClass theclass;
    var serializer = new DataContractSerializer(typeof(MyClass));

    using (Stream fileStream = File.Open("aaa.bin", FileMode.Open))
    {
      XmlDictionaryReaderQuotas xq = new XmlDictionaryReaderQuotas();
      XmlDictionaryReader binaryDictionarReader = XmlDictionaryReader.CreateBinaryReader(fileStream, xq);
      theclass = (MyClass)serializer.ReadObject(binaryDictionarReader);

    }
  }

}

}

[DataContract(IsReference = true)]
public class MyClass
{
  // need a parameterless constructor for serialization
  public MyClass()
  {
    MyDictionary = new Dictionary<string, string>() { { "AAA", "BBB" } , {"CCC", "DDD"} };
  }
  public Dictionary<string, string> MyDictionary { get; set; }<----no attribute
  public int aaaa = 3;<----no attribute 

}
Community
  • 1
  • 1
Patrick
  • 3,073
  • 2
  • 22
  • 60
  • 2
    You could do some regex replace. Not ideal but should work for the most part. – Dion V. Dec 21 '15 at 08:08
  • 1
    Possible duplicate of [Set DataContract and DataMember Without All the Attributes](http://stackoverflow.com/questions/7264748/set-datacontract-and-datamember-without-all-the-attributes) – Vadim Martynov Dec 21 '15 at 08:11
  • 2
    No, you cannot apply `[DataMember]` automatically to all properties - once you start using `[DataContract]` and `[DataMember]`, you **have to** explicitly put `[DataMember]` on all those properties you want to have in your serialization – marc_s Dec 21 '15 at 08:14
  • You can not do it out of the box, but for example PostSharp provides such functionality: http://doc.postsharp.net/example-dataattributes. Another question, how did you you create class with countless members? Autogeneration? Maybe you can adjust template – Uriil Dec 21 '15 at 08:20
  • @VadimMartynov Colour me dumb but I can't get to make it work the [DataContract(IsReference = true)] attribute. – Patrick Dec 21 '15 at 08:46
  • @marc_s I think you might be right. And so since I have class A with a member that is list of instances of class B, I'll have to mark [DataMember] all the members of the class B as well haven't I? – Patrick Dec 21 '15 at 08:53
  • 1
    @Patrick please read commants to the answer in linked question: Once you add DataContract attribute you must mark properties you want to serialize with DataMember attribute. Default serialization I described works only if you don't use DataContract attribute at all. – Vadim Martynov Dec 21 '15 at 09:43
  • Fine that's what I did. Thanx – Patrick Dec 21 '15 at 10:03
  • @VadimMartynov you might want to take a look at http://stackoverflow.com/questions/34393319/datacontract-serializazion-error thanx – Patrick Dec 21 '15 at 10:33

0 Answers0