Color结构的最优雅的XML序列化 [英] Most elegant xml serialization of Color structure

查看:80
本文介绍了Color结构的最优雅的XML序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个问题窃听我足够的计算器上注册。目前,如果我想序列颜色以XML字符串作为命名的颜色,或#RRGGBB,或者#aarrggbb,我这样做是这样的:

One problem bugged me enough to register on stackoverflow. Currently if I want to serialize Color to XML string as named color, or #rrggbb, or #aarrggbb, i do it like this:

[XmlIgnore()]
public Color color;

[XmlElement(ElementName = "Color")]
public String color_XmlSurrogate
{
  get { return MyColorConverter.SetColor(color); }
  set { color = MyColorConverter.GetColor(value); }
}

下面MyColorConverter不序列化,只我喜欢的方式。但是,这一切感觉就像一个杂牌组装电脑,额外的领域和所有。有没有一种方法,使其在更短的线路工作,也许用C#连接TypeDescriptor属性与XML相关?

Here MyColorConverter does serialization just the way I like it. But all this feels like a kludge, with additional field and all. Is there a way to make it work in less lines, maybe connecting TypeDescriptor with C# attributes related to xml?

推荐答案

这里的东西我使用序列化颜色结构的XML。这比阴影在我看来,首要颜色性能更好。任何建议表示欢迎。

Here's something I'm using for serializing the Color struct in XML. It's better than shadowing the primary Color property in my opinion. Any suggestions welcome.

XmlColor 类主要依靠隐含的运营商语言功能来提供关键数据tranformations。做不到这一点,类是基本没用。添加的功能,其他位轮出的类。

The XmlColor class relies primarily on the implicit operator language feature to provide the key data tranformations. Without this, the class is basically useless. Other bits of functionality were added to round out the class.

XmlColor 助手还提供了一个方便的方法来分离颜色成分。我加了阿尔法属性来显示这一点。请注意,如果它拍成一路攀升至255 阿尔法组件将不会被序列化

The XmlColor helper also provides a convenient way to separate color components. I added the Alpha property to show this. Notice the Alpha component won't be serialized if it's cranked all the way up to 255.

反序列化网​​站颜色值结合当前存储在实例中的阿尔法值。其中属性解析不应该的问题的顺序。如果阿尔法属性XML源代码中丢失,例如元件值将被用来设置阿尔法的水平。这无疑是错误的;然而,在XML序列化的情况下, XmlColor 类将与 Col​​or.Black 初始化设置阿尔法 255

Deserializing the Web color value combines the Alpha value currently stored in the instance. The order in which the attributes are parsed shouldn't matter. If the Alpha attribute is missing in the XML source, the instance component value will be used to set the Alpha level. This is arguably faulty; however, in the case of XML serialization, the XmlColor class will initialized with Color.Black setting the Alpha to 255.

我工作了VS2010环境和建筑对.NET 4的我也没办法了code是如何兼容previous版本。

I'm working out of the VS2010 environment and building against .Net 4. I have no idea how compatible the code is with previous versions.

下面是应该被序列化到XML的例子属性:

Here's an example property that should be serialized to XML:

    [XmlElement(Type=typeof(XmlColor))]
    public Color MyColor { get; set; }

这里的 XmlColor 辅助类:

public class XmlColor
{
    private Color color_ = Color.Black;

    public XmlColor() {}
    public XmlColor(Color c) { color_ = c; }


    public Color ToColor()
    {
        return color_;
    }

    public void FromColor(Color c)
    {
        color_ = c;
    }

    public static implicit operator Color(XmlColor x)
    {
        return x.ToColor();
    }

    public static implicit operator XmlColor(Color c)
    {
        return new XmlColor(c);
    }

    [XmlAttribute]
    public string Web
    {
        get { return ColorTranslator.ToHtml(color_); }
        set {
            try
            {
                if (Alpha == 0xFF) // preserve named color value if possible
                    color_ = ColorTranslator.FromHtml(value);
                else
                    color_ = Color.FromArgb(Alpha, ColorTranslator.FromHtml(value));
            }
            catch(Exception)
            {
                color_ = Color.Black;
            }
        }
    }

    [XmlAttribute]
    public byte Alpha
    {
        get { return color_.A; }
        set { 
            if (value != color_.A) // avoid hammering named color if no alpha change
                color_ = Color.FromArgb(value, color_); 
        }
    }

    public bool ShouldSerializeAlpha() { return Alpha < 0xFF; }
}

这篇关于Color结构的最优雅的XML序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆