c# - Most elegant xml serialization of Color structure -


A problem bugged me to register on the stackoverflow sufficiently if I color the color to the name of the XML string I want to do it, or #RRGDBB, or #agreggb, I do it like this:

  [XmlIgnore ()] color of public color; [XmlElement (Elementanum = "Color")] Get public string color_XmlSurrogate {get {MyColorConverter.SetColor (color); } Set {color = MyColorConverter.GetColor (value); }}  

Here MyColorConverter does the serialization, the way I like it. But it all feels like an area, with extra zones and all. Is this a way to work in lower lines, maybe connecting TypeDescriptor with XML # related to C # attributes?

Here's something I want to sort in the color strat in XML I am using it. It is better to hide the primary color property in my opinion. Any suggestions are welcome.

To provide the main code tranformations, the XmlColor class depends primarily on the underlying operator language. Without it, the class is basically useless.

XmlColor Assistant also provides a convenient way to separate color components. I've added the alpha property to show it the alpha component will not be serialized, if it is cranked by 255 all the way.

Web The list of color values ​​is stored in the alpha value current in which the attributes are taught in the specifications, there should not be any difference. If the alpha attribute is missing in the XML source, then the component value of the instance will be used to set the alpha level; this is debatable; However, in the case of XML serialization, the XmlColor class will be set from Color.Black to alpha and up to 255.

I am working out of building against VS2010 environment and .Net 4. I do not know that the code is compatible with previous versions.

Here is an example property that should be serialized in XML:

  [XmlElement (Type = Type = Typx (XmlColor)]] Public color MyColor {get; Set; }  

Here is the XmlColor assistant category:

  public class XmlColor {personal color color_ = Color.black; Public XmlColor () {} Public XmlColor (color c) {color_ = c; } Public color ToColor () {return color_; } Public color to zero (color C) {color_ = c; } Public stable built-in operator color (XmlColor x) {return x.ToColor (); } Public stable built-in operator XmlColor (Color C) {New XmlColor (C) back; } [XmlAttribute] Public string web {get {Return ColorTranslator.ToHtml (color_); } Set {try (if (alpha == 0xFF) // Protect the named color value if possible, color_ = ColorTranslator.FromHtml (value); Else color_ = color.formArgb (alpha, changer.formHTML (value)) ;} Catch (exception) {color_ = Color.Black;}}} [XmlAttribute] Set the public byte alpha {get {return color_.A;} {if (value! = Color_.A) // Color of the name Humming Avoid if there is no alpha change color_ = Color.FromArgb (value, color_);}} Public hair should be aliases (alpha & lt; 0xFF;}}  

Comments