How to XmlSerialize System.Drawing.Font class

后端 未结 5 1885
心在旅途
心在旅途 2021-01-11 17:15

The classSystem.Drawing.Font is not XML Serializable since it doesn\'t have a default (empty) constructor.
Is there some work around or alternative way to

5条回答
  •  太阳男子
    2021-01-11 18:16

    I use a serializable font, somewhat different from Elad's.

    In my serializable data-entities I hide ([XmlIgnore]) the property with the Font type and expose the property with the SerializableFont type, which is "eaten" by the serializer.

    Note that this is applicable to the XmlSerializer only.

    /// 
    /// Font descriptor, that can be xml-serialized
    /// 
    public class SerializableFont
    {
        public string FontFamily { get; set; }
        public GraphicsUnit GraphicsUnit { get; set; }
        public float Size { get; set; }
        public FontStyle Style { get; set; }
    
        /// 
        /// Intended for xml serialization purposes only
        /// 
        private SerializableFont() { }
    
        public SerializableFont(Font f)
        {
            FontFamily = f.FontFamily.Name;
            GraphicsUnit = f.Unit;
            Size = f.Size;
            Style = f.Style;
        }
    
        public static SerializableFont FromFont(Font f)
        {
            return new SerializableFont(f);
        }
    
        public Font ToFont()
        {
            return new Font(FontFamily, Size, Style,
                GraphicsUnit);
        }
    }
    

提交回复
热议问题