Serialization and deserialization of properties such as WPF fonts, colors, and so on

Source: Internet
Author: User

Some of the properties that WPF takes, such as System.Windows.Media.Brush, System.Windows.Media.FontFamily, System.Windows.FontWeight and so on do not have the direct serialization ability, this paper designs the class which is called Propertyserializatedictionary, realizes the serialization and the deserialization to the different class different object public attribute. This class inherits from Dictionary<string, Typeandvalue>, IXmlSerializable, and invokes the self implementation class Typeandvalue.

Code:

<summary>///classes used to record objects and types///used in Propertyserializatedictionary///unit tests: Propertyseriali Zatedictionarytest///</summary> public class Typeandvalue {public Typeandvalu     
           E (Type type, object value) {type = type;     
       Value = value;     
       Public Typeandvalue () {} public type type {get; set;}     
         
       Public object Value {get; set;}     
           public override bool Equals (object obj) {typeandvalue Tav = obj as Typeandvalue;     
           if (Tav = = null) {return false;     
         
           var equalsmehtod = Type.getmethod ("Equals"); if (Tav. Type.fullname = = this. Type.fullname && ((bool) Equalsmehtod.invoke (TAV. Value, new object[]{this.     
  Value}))             ) {return true;     
         
           };     
       return false; }
<summary>///serialization and deserialization of public properties of different classes of objects///serializable, deserialized WPF system color, Brush, FontFamily, fontweight Cannot recognize collection type and struct? Type///</summary>///<typeparam name= "T" ></typeparam> public class PropertyS Erializatedictionary:dictionary<string, Typeandvalue&gt, ixmlserializable {public system.xml.     
        Schema.xmlschema GetSchema () {return null; public void ReadXml (System.Xml.XmlReader reader) {if (reader).     
            Isemptyelement) {return; Reader.     
            Read (); String typestring = reader.     
         
            GetAttribute ("Type"); while (reader. NodeType!= System.Xml.XmlNodeType.EndElement) {string key = reader.     
         
                Name; Type TPE = Type.GetType (typestring); propertyinfo[] props = TPE.     
         
                GetProperties (); This[key] = new Typeandvalue (TPE, TPE. GetConstructor (new type[] {}).     
         
                Invoke (null)); if (!reader. Isemptyelement) {//Fill attribute value reader.     
                    Readstartelement (); if (reader. NodeType!= System.Xml.XmlNodeType.EndElement) {foreach (Propertyin Fo pi in props) {if (pi.     
                                Propertytype.fullname = = "System.Windows.Media.Brush") {     
                                System.Windows.Media.BrushConverter brushconverter = new System.Windows.Media.BrushConverter (); Pi. SetValue (This[key]. Value, (System.Windows.Media.Brush) brushconverter.convertfromstring (reader. Readelementstring (pi.     
    Name)), null);                            Converts a read string based on the type of the property} else if (pi.     
                                Propertytype.fullname = = "System.Windows.Media.FontFamily") {     
                                System.Windows.Media.FontFamilyConverter fontfamilyconverter = new System.Windows.Media.FontFamilyConverter (); Pi. SetValue (This[key]. Value, (System.Windows.Media.FontFamily) fontfamilyconverter.convertfromstring (reader. Readelementstring (pi.     
                            Name)), null); else if (pi.     
                                Propertytype.fullname = = "System.Windows.FontWeight") {     
                                System.Windows.FontWeightConverter fontfamilyconverter = new System.Windows.FontWeightConverter (); Pi. SetValue (This[key]. Value, (System.Windows.FontWeight) fontfamilyconverter.convertfromstring (Reader. Readelementstring (pi.     
                            Name)), null);     
                                } else { Pi. SetValue (This[key]. Value, Convert.changetype (reader. Readelementstring (pi. Name), Pi.     
                            PropertyType), NULL); }}} READER.R     
            EAD ();     
            }} public void WriteXml (System.Xml.XmlWriter writer) { if (this.     
            Keys.count < 1) {return; foreach (string name in Keys) {typeandvalue TANDV = NE     
         
                W Typeandvalue (); Writer.     
         
                WriteStartElement (name); if (this.     
        TryGetValue (name, out TANDV))        {writer. WriteAttributeString ("Type", Tandv.)     
                    Type.fullname); Stores the public properties of the type propertyinfo[] props = Tandv.     
         
                    Type.getproperties ();     
                        Writes each property name and property value of an object to the XML file foreach (PropertyInfo prop in props) { < property name > attribute value </property name > Writer. WriteElementString (Prop. Name, Prop. GetValue (TANDV. Value, NULL).     
                    ToString ()); }} writer.     
            WriteEndElement (); } public override bool Equals (object obj) {Propertyse     
            Rializatedictionary dic = obj as propertyserializatedictionary;     
            if (dic = null) {return false; } if (dic. Count!= this.     
       Count)     {return false; foreach (String key in DiC. Keys) {if (!this[key].     
                Equals (Dic[key])) {return false;     
        } return true; }     
    }

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/net/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.