I have an enum that is serialized and sent over the wire. Requirements have evolved and I need to support additional values that may be defined in other assemblies, but I can't break binary compatibility for the who-knows-what that is in the wild.
My initial idea was to replace this enum with a enum-like struct wrapping an integer and use a surrogate to serialize it as an integer:
public readonly struct SomeFlag
{
private readonly int _value;
public SomeFlag(int value) { _value = value; }
public static implicit operator int(SomeFlag type)
{
return type._value;
}
public static implicit operator SomeFlag(int value)
{
return new SomeFlag(value);
}
}
...
TypeModel
.Add(typeof(SomeFlag), false)
.SetSurrogate(typeof(int));
When I try to serialize SomeFlag, I get an error: Data of this type has inbuilt behaviour, and cannot be added to a model in this way: System.Int32
Aside from replacing the structs in my models and code with plain integers, is there any way to make a non-primitive type serialize as a primitive?