开发者

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

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);
开发者_StackOverflow中文版    }

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.


XML serialization only works with public properties, so you would need to expose a public property that sets/gets the value.

As an alternative, you can implement IXmlSerializable to manually serialise the structure.

This is only an issue with XML serialisation. The binary serialiser will serialise private fields as well as public ones.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜