La classe System.Drawing.Font
n’est pas sérialisable XML car elle n’a pas de constructeur par défaut (vide).
Existe-t-il une solution de rechange ou un autre moyen de sérialiser Font
néanmoins?
Edit: J’ai mis à jour le code selon la suggestion de Regent d’utiliser FontConverter
, tout en préservant la possibilité d’utiliser SerializableFont
tant que Font
standard.
public class SerializableFont { public SerializableFont() { FontValue = null; } public SerializableFont(Font font) { FontValue = font; } [XmlIgnore] public Font FontValue { get; set; } [XmlElement("FontValue")] public ssortingng SerializeFontAtsortingbute { get { return FontXmlConverter.ConvertToSsortingng(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 ssortingng ConvertToSsortingng(Font font) { try { if (font != null) { TypeConverter converter = TypeDescriptor.GetConverter(typeof(Font)); return converter.ConvertToSsortingng(font); } else return null; } catch { System.Diagnostics.Debug.WriteLine("Unable to convert"); } return null; } public static Font ConvertToFont(ssortingng fontSsortingng) { try { TypeConverter converter = TypeDescriptor.GetConverter(typeof(Font)); return (Font)converter.ConvertFromSsortingng(fontSsortingng); } catch { System.Diagnostics.Debug.WriteLine("Unable to convert"); } return null; } }
Utilisation: Lorsque vous avez une propriété Font
, déclarez-la en tant que SerializableFont
. Cela lui permettra d’être sérialisé, tandis que la conversion implicite gérera la conversion pour vous.
Au lieu d’écrire:
Font MyFont {get;set;}
Écrire:
SerializableFont MyFont {get;set;}
Une suggestion sur la manière de procéder en implémentant une classe wrapper sérialisable est donnée sur la page MSDN de la classe Font .
J’utilise une police sérialisable, quelque peu différente de celle d’ Elad .
Dans mes entités de données sérialisables, je masque ( [XmlIgnore]
) la propriété avec le type de Font
et l’expose avec le type SerializableFont
, qui est “mangé” par le sérialiseur.
Notez que cela s’applique uniquement à XmlSerializer
.
/// /// Font descriptor, that can be xml-serialized /// public class SerializableFont { public ssortingng 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); } }
System.Drawing.Font
a une classe FontConverter
associée et je la convertirais manuellement:
[Serializable] public class SerializableFont { public SerializableFont() { this.Font = null; } public SerializableFont(Font font) { this.Font = font; } [XmlIgnore] public Font Font { get; set; } [XmlElement("Font")] public ssortingng FontSsortingng { get { if (font != null) { TypeConverter converter = TypeDescriptor.GetConverter(typeof(Font)); return converter.ConvertToSsortingng(this.Font); } else return null; } set { TypeConverter converter = TypeDescriptor.GetConverter(typeof(Font)); this.Font = converter.ConvertFromSsortingng(value); } } }
Essayez le DataContractSerializer.
Font fnt = new Font("Arial", 1); MemoryStream data = new MemoryStream(); DataContractSerializer dcs = new DataContractSerializer(typeof(Font), new[] { typeof(FontStyle), typeof(GraphicsUnit) }); dcs.WriteObject(data, fnt); ssortingng xml = Encoding.UTF8.GetSsortingng(data.ToArray());