I have a class that is effectively a object based enum. There is a static set of objects exposed by the class, and everything uses these same instances. eg (Note the private constructor)
[DataContract]
public class FieldType
{
public static readonly FieldType Default = new FieldType(1, "Default");
public static readonly FieldType Name = new FieldType(2, "Name");
public static readonly FieldType Etc = new FieldType(3, "Etc");
private FieldType(uint id, string name)
{
Id = id;
Name = name;
}
[DataMember] public uint Id { get; private set; }
[DataMember] public string Name { get; private set; }
//snip other properties
}
This works great until I have to serialise across WCF. The DataContractSerializer
creates new objects by bypassing the constructor. This results in a valid FieldType
object, but it is a new instance that is not one of my static instances. This makes reference comparisons against the known static values fail.
Is there any way to override the serialisation behaviour for a class so that I create the object instance instead of populating an instance supplied to me?
Name
is used for static and non-static, meaning: it won't compile as-is) – Hakon