How can I have a serializable struct that wraps it's self as an int32 implicitly? in C#?
- by firoso
Long story short, I have a struct (see below) that contains exactly one field:
private int value;
I've also implemented implicit conversion operators:
public static implicit operator int(Outlet val)
{
return val.value;
}
public static implicit operator Outlet(int val)
{
return new Outlet(val);
}
I've implemented all of the following :
IComparable, IComparable<Cart>, IComparable<int>, IConvertible, IEquatable<Cart>, IEquatable<int>, IFormattable
I'm at a point where I really have no clue why, but whenever I serialize this object, I get no value. For instance, with XmlSerialization:
<Outlet />
Also, I'm not solely concerned about XmlSerialization, I'm concerned about ALL serialization (binary for instance) How can I ensure that this serializes properly?
NOTE: I did this because mapping an int,int dictionary seemed rather poorly typed to me when explicit objects with validation behavior were desired.