c# - Wrong data got back from deserializing an object with custom type cast operator -
i have class designed perform uint16. instead of doing: uint16 myprop, can use address myprop, more descriptive.
here's snippet:
public class address { public uint16 address { get; set; } private static uint16 add; public static implicit operator uint16(address address) { return add; } public static implicit operator address(uint16 i) { address temp = new address(i); return temp; } public address(uint16 value) { address = value; add = value; } public address() { } }
then have property: public address myaddress { get; set; }
the rest of code works perfectly.
this class need serialized , de-serialized xml.
after serialization, have in xml
<myaddress> <address>7</address> </myaddress>
but after desterilized xml file, cannot myaddress property 7.
maybe weird question... idea?
your conversion operator uint16
broken - it's using static variable, set whichever value happens have been constructed recently. did not think odd ignoring value being passed conversion operator?
it's not clear why you've got static variable @ all, suspect should rid of it, , conversion opperator should be:
public static implicit operator uint16(address address) { return address.address; }
you should rename property follow .net naming conventions... , ideally change name anyway, it's weird address
type have address
property. it's not clear type meant represent anyway, perhaps value
@ least better?
Comments
Post a Comment