How can I have a serializable struct that wraps it's self as an int32 implicitly? in C#?

Posted by firoso on Stack Overflow See other posts from Stack Overflow or by firoso
Published on 2010-06-11T01:08:57Z Indexed on 2010/06/11 1:12 UTC
Read the original article Hit count: 273

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about .NET

Related posts about serialization