Serializing System.Drawing.Color in .NET

匆匆过客 提交于 2019-12-31 01:58:05

问题


I've used the default .NET serialization for a class with a System.Drawing.Color member. The code is now in use by people, and I need to add an extra member to the class, but still deserialize older versions.

So I tried the standard way of doing this: The ISerializable interface, using SerializationInfo methods to get the int and string members.

The problem: My class also has a System.Drawing.Color member, but SerializationInfo doesn't provide a "GetColor" method read this data type. I've tried getting it as an int and as a string, and casting it to System.Drawing.Color, but no luck.

Does anyone know how to deserialize a System.Drawing.Color from SerializationInfo?


回答1:


I have used something like this in the past.

 <Xml.Serialization.XmlIgnore()> Public BackColour As Drawing.Color

        Public Property xmlBackColour() As Integer
            Get
                Return BackColour.ToArgb
            End Get
            Set(ByVal value As Integer)
                BackColour = Drawing.Color.FromArgb(value)
            End Set
        End Property



回答2:


Using ISerializable is not the recommended way to deal with versioning. The [OptionalField] is, the ins and outs are well described in this MSDN library article.

Answering your question: SerializationInfo.GetValue("fieldName", typeof(Color)) ought to give you the color. You'll need to cast the return value.



来源:https://stackoverflow.com/questions/2480719/serializing-system-drawing-color-in-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!