This project has moved and is read-only. For the latest updates, please go here.
1
Vote

DataContract attribute incorrectly inherited

description

There was a discussion for this here https://json.codeplex.com/discussions/357850

A type deriving from a type decorated with the [DataContract] attribute causes Json.NET to assume opt in serialization for the deriving type despite the attribute being intended to not be inherited.

From MSDN on DataContract:

[AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Struct|AttributeTargets.Enum, Inherited = false, AllowMultiple = false)]
public sealed class DataContractAttribute : Attribute

http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractattribute(v=vs.110).aspx

comments

johnhomes wrote Mar 5, 2016 at 6:45 AM

You need to put KnownType attribute on Manager:
[DataContract]
[KnownType(typeof(Medium))]
[KnownType(typeof(Small))]
public class Manager
{
[DataMember]
public BigBase[] enemies;
}
Because it's the Manager which has an array of BigBase whose elements might be the derived classes as well. So the DataContractSerializer will know what to expect from the array when serializing and deserializing Manager object (and it's all DataMember).
Thanks
JSON Developer
http://www.qwiknumbers.com/sky-customer-service-number