C#: instantiating classes from XML

前端 未结 8 789
情歌与酒
情歌与酒 2021-02-04 09:05

What I have is a collection of classes that all implement the same interface but can be pretty wildly different under the hood. I want to have a config file control which of the

8条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-04 09:39

    I would also suggest Xml serialization as others have already mentioned. Here is a sample I threw together to demonstrate. Attributes are used to connect the names from the Xml to the actual property names and types in the data structure. Attributes also list out all the allowed types that can go into the Things collection. Everything in this collection must have a common base class. You said you have a common interface already -- but you may have to change that to an abstract base class because this code sample did not immediately work when Thing was an interface.

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Xml.Serialization;
    using System.IO;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main()
            {
                string xml =
                    "" + 
                    "" +
                    "" + 
                    "  " +
                    "  " +
                    "" +
                    "";
                StringReader sr = new StringReader(xml);
                XmlSerializer xs = new XmlSerializer(typeof(ThingCollection));
                ThingCollection tc = (ThingCollection)xs.Deserialize(sr);
    
                foreach (Thing t in tc.Things)
                {
                    Console.WriteLine(t.ToString());
                }
            }
        }
    
        public abstract class Thing
        {
        }
    
        [XmlType(TypeName="class1")]
        public class SomeThing : Thing
        {
            private string pn1;
            private string pn2;
    
            public SomeThing()
            {
            }
    
            [XmlAttribute("prop1")]
            public string PropertyNumber1
            {
                get { return pn1; }
                set { pn1 = value; }
            }
    
            [XmlAttribute("prop2")]
            public string AnotherProperty
            {
                get { return pn2; }
                set { pn2 = value; }
            }
        }
    
        [XmlType(TypeName="class2")]
        public class SomeThingElse : SomeThing
        {
            private int answer;
    
            public SomeThingElse()
            {
            }
    
            [XmlAttribute("prop3")]
            public int TheAnswer
            {
                get { return answer; }
                set { answer =value; }
            }
        }
    
        [XmlType(TypeName = "config")]
        public class ThingCollection
        {
            private List things;
    
            public ThingCollection()
            {
                Things = new List();
            }
    
            [XmlArray("stuff")]
            [XmlArrayItem(typeof(SomeThing))]
            [XmlArrayItem(typeof(SomeThingElse))]
            public List Things
            {
                get { return things; }
                set { things = value; }
            }
        }
    }
    

提交回复
热议问题