, , JsonTypeReflector.cs :
public const string TypePropertyName = "$type";
: , type , SerializationBinder Class , this , .
, . -, ( Geometry) ,
[DataContract(Name = "Entity", Namespace = "myContract.com/dto")]
public abstract class Entity
{
[DataMember(Name = "__type", Order = 0)]
public string EntityTypeName
{
get
{
var typeName = GetType().Name;
if (Attribute.IsDefined(GetType(), typeof(DataContractAttribute)))
{
var attribute = GetType().GetCustomAttributes(typeof(DataContractAttribute), true).FirstOrDefault() as DataContractAttribute;
if (attribute != null) typeName = typeName + ":" + attribute.Namespace;
}
return typeName;
}
}
}
Geometry :
[DataContract(Name = "Geometry", Namespace = "myContract.com/dto")]
[KnownType(typeof(Point))]
[KnownType(typeof(Line))]
public class Geometry : Entity
{
}
, JsonSerializerSettings.TypeNameHandling TypeNameHandling.None, $type.
var point = new Point { X = 23133.75569999963, Y = 21582.385849999264 };
var jsonPoint = JsonConvert.SerializeObject(point, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.None,
});
Console.WriteLine(jsonPoint);
{\"__type\":\"Point:myContract.com/dto\",\"x\":23133.75569999963,\"y\":21582.385849999264}
P.S.
, DataMember.Order, :
[DataContract(Name = "Point", Namespace = "myContract.com/dto")]
public class Point : Geometry
{
[DataMember(Name = "x",Order = 1)]
public double X { get; set; }
[DataMember(Name = "y", Order = 2)]
public double Y { get; set; }
}
[DataContract(Name = "Line", Namespace = "myContract.com/dto")]
public class Line : Geometry
{
[DataMember(Name = "start", Order = 1)]
public Point Start { get; set; }
[DataMember(Name = "end", Order = 2)]
public Point End { get; set; }
}