Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Edit:</strong> I updated the code according to <a href="https://stackoverflow.com/questions/1940127/how-to-xmlserialize-system-drawing-font-class/1940307#1940307">Regent</a> suggestion to use <code>FontConverter</code>, while preserving the ability to use the <code>SerializableFont</code> as regular <code>Font</code>.</p> <pre><code>public class SerializableFont { public SerializableFont() { FontValue = null; } public SerializableFont(Font font) { FontValue = font; } [XmlIgnore] public Font FontValue { get; set; } [XmlElement("FontValue")] public string SerializeFontAttribute { get { return FontXmlConverter.ConvertToString(FontValue); } set { FontValue = FontXmlConverter.ConvertToFont(value); } } public static implicit operator Font(SerializableFont serializeableFont) { if (serializeableFont == null ) return null; return serializeableFont.FontValue; } public static implicit operator SerializableFont(Font font) { return new SerializableFont(font); } } public static class FontXmlConverter { public static string ConvertToString(Font font) { try { if (font != null) { TypeConverter converter = TypeDescriptor.GetConverter(typeof(Font)); return converter.ConvertToString(font); } else return null; } catch { System.Diagnostics.Debug.WriteLine("Unable to convert"); } return null; } public static Font ConvertToFont(string fontString) { try { TypeConverter converter = TypeDescriptor.GetConverter(typeof(Font)); return (Font)converter.ConvertFromString(fontString); } catch { System.Diagnostics.Debug.WriteLine("Unable to convert"); } return null; } } </code></pre> <p>Usage: When you have a <code>Font</code> property, declare it as <code>SerializableFont</code>. This will allow it to be serialized, while the implicit cast will handle the conversion for you.</p> <p>Instead of writing:</p> <pre><code>Font MyFont {get;set;} </code></pre> <p>Write:</p> <pre><code>SerializableFont MyFont {get;set;} </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload