A specialized type definition is necessary in class when serialization is involved.(涉及Serialzation的类...

    ///
    /// Summary description for Student.
    ///

    public class Student

    {

        public int Number

        {
            get{return this.number;}
            set{this.number = value;}
        }

        public string Name
        {
            get{return this.name;}
            set{this.name = value;}
        }

        private int number;
        private string name;
    }
   
    ///
    /// Summary description for Group.
    ///

    public class Group
    {
        public string Name
        {
            get{return this.name;}
            set{this.name = value;}
        }
       
        // Notice: If this public specialized type definition does not exist in this class, then serializing/deserializing would fail to cast.  For a class it is not necessary actually.

        public Student member; 

        private ArrayList members = new ArrayList();
        public ArrayList Members
        {
            get{return this.members;}
            set{this.members = value;}

        }

 

        // Or use Generic to give out a specialized type definition.

        //private List members = new List();
        //public List Members
        //{
        //    get{return this.members;}
        //    set{this.members = value;}
        //}

 
        private string name;     
    }

...

     private static void TestSerialization(string fileName)
        {
            Group group = new Group();
            group.Name = "Equity";

            Student o = new Student();
            o.Name = "D1";
            o.Number = 254;
            Student student = new Student();
            student.Name = "D2";
            student.Number = 255;

            group.Members.Add(o);
            group.Members.Add(student);
            XmlSerializer s = new XmlSerializer(typeof(Group));

            XmlTextWriter xw = new XmlTextWriter(fileName, Encoding.Default);
            s.Serialize(xw, group);
            xw.Close();
        }

        private static void TestDeserialization(string fileName)
        {

            XmlSerializer s = new XmlSerializer(typeof(Group));

            XmlTextReader xr = new XmlTextReader(fileName);
            object obj = s.Deserialize(xr);
            xr.Close();
            Group g = obj as Group;
            Console.WriteLine(g.Name);
            Student st = g.Members[0] as Student;
            Console.WriteLine(st.Number.ToString());

        }

 

转载于:https://www.cnblogs.com/imdiego/archive/2007/02/28/659272.html

你可能感兴趣的:(A specialized type definition is necessary in class when serialization is involved.(涉及Serialzation的类...)