C # - Serialization of DataContract - base classes, inheritance and overrides

Good afternoon everyone

I am an independent game developer who in the past worked mainly with XNA and, on the other hand, commercial tools. However, the reach of XNA is quite limited, and I am creating a cross-platform level of abstraction to define multiple platforms.

To shorten the long story, I needed XML serialization, which is more widely available than [Serializable], and I pointed to data contracts. I have done a lot of research, but I can not find any good information about the elements of the system related to inheritance and overrides.

The essence of my question ...

[DataContract] public class Node { private string name; public string Name { get { return name; } set { name = value; } } public virtual float Rotation { get { return 0f; } set { } } } [DataContract] public class FancyNode : Node { private float rotation; public override float Rotation { get { return rotation; } set { rotation = value; } } } 

If I serialize "FancyNode", will the "Rotation" be correctly serialized and will the "Name" be serialized?

Follow-up question: I wanted to ask earlier, but I could not remember at that time. How does the serializer handler override the properties of [IgnoreDataMember]? For example...

 [DataContract] public class Simple { [IgnoreDataMember] public virtual string Value { get { return ""; } set { } } } [DataContract] public class Complex : Simple { private string value; public override string Value { get { return value; } set { this.value = value; } } } 

Will the "Value" in the "Complex" be serialized? Some answers suggest that if the [DataMember] tags are not used, all properties will be serialized. If so, does the [IgnoreDataMember] attribute of the base class have any bearing?

+7
source share
4 answers

As far as I know, DataContract is a β€œpick” method in serialization, i.e. the material does not serialize unless you decorate it (tell the serializer which you want to serialize).

So, for the example above, you need to add [DataMember] to the properties you would like to serialize

With the standard Serializable attribute, the serializer scans all the fields and ignores only those that you mark as NonSerialized

Some examples here:

http://jamescbender.azurewebsites.net/?p=651

Check the notes section for information on what gets a serialized and rough outline of what is happening:

http://msdn.microsoft.com/en-us/library/ms733127.aspx

Edit: I also see no reason why any of the fields marked as [DataMember] will not be serialized correctly. The DataContract method for serialization can also deal with circular references - something that sometimes causes serialization:

http://msdn.microsoft.com/en-us/library/hh241056.aspx

+5
source

Some of the thoughts presented in several answers are unclear. Please let me clean them. First, a serializer that uses a DataContract can be used to serialize types that are not decorated with a DataContract. You only need to report this to the serailizer program.

For example, the DataContractJsonSerializer class serializes the type into a JSON object (usually a string). All you have to say is the type of object you are serializing, and then any other types that it can reference:

 var serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(MyClass), new Type[] { Type1, Type2, Type3 }); 

The serializer automatically serializes each public field and type property. For more information see http://msdn.microsoft.com/en-us/ms733127

DataContractAttribute Labeled Types

After you mark the type with DataContractAttribute , you turn this type into an automatically known type (which you do not need to specify as a child type), and you turn it into properties and fields in opt-in mode.

Therefore, you must decorate each field or property that you want to serialize with DataMemberAttribute . This means that IgnoreDataMemberAttribute useless. This means that the serializer will not look for it, because everything that is not marked with the DataMemberAttribute symbol is automatically ignored.

Unmarked DataContractAttribute Types

When serializing a type that does not use a DataContractAttribute, as mentioned earlier, each public property or field will be serialized. Therefore, IgnoreDataMemberAttribute is used here to prevent the serialization of a property or field.

+5
source

You must include the [DataMember] in the properties to be contracted.

 [DataContract] public class FancyNode : Node { private float rotation; [DataMember] public override float Rotation { get { return rotation; } set { rotation = value; } } } 

Note that the Windows Communication Foundation (WCF) uses the Data Serializer to serialize and deserialize the data (converts it to and from an XML file). So you are still using Xml Serialization.

+1
source

No, it will not be serialized due to the Opt-In approach, you must explicitly apply the DataMember to the base class. Data serialization does not work automatically if you inherit a child class

0
source

All Articles