简体   繁体   中英

Should I add the DataMember attribute to overridden properties in a derived class?

I have a couple of classes that look like this:

[DataContract]
public class A {

   [DataMember(Order = 1)]
   public virtual string SomeString{
       get;
       set;
   }
}

[DataContract]
public class B : A {

   [DataMember(Order = 1)]
   public override string SomeString{
       get{ // Do something }
       set{ // Do something} 
   }
}

Do I need to include the DataMember attribute on SomeString in the derived class? Is the data contract for the derived class treated in isolation to the base class?

I followed jdweng's suggestion of actually inspecting the serialized data with and without the DataMember attribute on the derived class. The property SomeString was serialized for the derived class without the DataMember attribute on the SomeString property.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM