• 序列化辅助类


    该类用来序列化一些不支持序列化对象或者简化序列化对象,部分代码来自网上。
       public  class SerialHelper
        {
            #region Serialization Helpers
            #region Helper classes/structs
            
            public struct XmlFont
            {
                public string FontFamily;
                public GraphicsUnit GraphicsUnit;
                public float Size;
                public FontStyle Style;
    
                public XmlFont(Font f)
                {
                    FontFamily = f.FontFamily.Name;
                    GraphicsUnit = f.Unit;
                    Size = f.Size;
                    Style = f.Style;
                }
    
                public Font ToFont()
                {
                    return new Font(FontFamily, Size, Style, GraphicsUnit);
                }
            }
            public struct XmlRect
            {
                public int Left, Top, Width, Height;
    
                public XmlRect(Rectangle rectangle)
                {
                    this.Left = rectangle.Left;
                    this.Top = rectangle.Top;
                    this.Width = rectangle.Width;
                    this.Height = rectangle.Height;
                }
    
                public Rectangle ToRectangle()
                {
                    return new Rectangle(Left, Top, Width, Height);
                }
            }
            #endregion
            public enum ColorFormat
            {
                NamedColor,
                ARGBColor,
    
            }
            public static XmlRect SerializeRect(Rectangle rectangle)
            {
                return new XmlRect(rectangle);//string.Format("{0}:{1}:{2}:{3}", rectangle.Left,rectangle.Top,rectangle.Width,rectangle.Height);
            }
    
            public static Rectangle DeserializeRect(XmlRect xmlRect)
            {
                return xmlRect.ToRectangle();
    
            }
            public static string SerializeColor(Color color)
            {
                if (color.IsNamedColor)
                    return string.Format("{0}:{1}", ColorFormat.NamedColor, color.Name);
                else
                    return string.Format("{0}:{1}:{2}:{3}:{4}", ColorFormat.ARGBColor, color.A, color.R, color.G, color.B);
            }
    
            public static Color DeserializeColor(string color)
            {
                byte a, r, g, b;
    
                string[] pieces = color.Split(new char[] { ':' });
    
                ColorFormat colorType = (ColorFormat)Enum.Parse(typeof(ColorFormat), pieces[0], true);
    
                switch (colorType)
                {
                    case ColorFormat.NamedColor:
                        return Color.FromName(pieces[1]);
    
                    case ColorFormat.ARGBColor:
                        a = byte.Parse(pieces[1]);
                        r = byte.Parse(pieces[2]);
                        g = byte.Parse(pieces[3]);
                        b = byte.Parse(pieces[4]);
    
                        return Color.FromArgb(a, r, g, b);
                }
                return Color.Empty;
            }
    
            public static XmlFont SerializeFont(Font font)
            {
                return new XmlFont(font);
            }
    
            public static Font DeserializeFont(XmlFont font)
            {
                return font.ToFont();
            }
    
            #endregion
        }

    用法示例

         [Browsable(true)]
            [XmlIgnore()]
            public Font FontObject
            {
                get => textFont;
                set => textFont = value;
            }
    
            [Browsable(false)]
            [XmlElement("FontObject")]
            public SerialHelper.XmlFont XmlFontObject
            {
                get
                {
                    return SerialHelper.SerializeFont(FontObject);
                }
                set
                {
                    FontObject = SerialHelper.DeserializeFont(value);
                }
            }
         [Browsable(true)]
            [XmlIgnore()]
            public Rectangle Rect { get => rect; set => rect = value; }
            [Browsable(false)]
            [XmlElement("RectObject")]
            public SerialHelper.XmlRect XmlRectObject
            {
                get
                {
                    return SerialHelper.SerializeRect(Rect);
                }
                set
                {
                    Rect = SerialHelper.DeserializeRect(value);
                }
            }
         [Browsable(true)]
            [XmlIgnore()]
            public Color BackColor { get => backColor; set => backColor = value; }
            [Browsable(false)]
            [XmlElement("BackColorObject")]
            public string XmlBackColorObject
            {
                get
                {
                    return SerialHelper.SerializeColor(BackColor);
                }
                set
                {
                    BackColor = SerialHelper.DeserializeColor(value);
                }
            }
  • 相关阅读:
    轻松实现内容的无缝平滑滚动
    世界“新七大奇迹”揭晓 中国长城成功入选
    Blog_Backup:绿色博客备份工具
    【Vista中系统准备工具存在隐患,自动清除用户资料】
    Oracle大连全球支持中心运营就在我们公司前面!
    ASP.NET2.0下使用AJAX调用Webservice的方法
    配合AJAX天气预报的WebService
    Google兑现承诺你可以从google上删除你的搜索记录了
    全体注意:@live.com 形势不妙,可能三周内被收回
    国内软件联盟抨击微软以非正规手段推文档标准
  • 原文地址:https://www.cnblogs.com/zhaogaojian/p/8526516.html
Copyright © 2020-2023  润新知