C#: instantiating classes from XML

前端 未结 8 770
情歌与酒
情歌与酒 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:43

    It may be easier to serialise the classes to/from xml, you can then simply pass the XmlReader (which is reading your config file) to the deserializer and it will do the rest for you..

    This is a pretty good article on serialization

    Edit

    One thing I would like to add, even though reflection is powerful, it requires you to know some stuff about the type, such as parameters etc.

    Serializing to XML doesnt need any of that, and you can still have type safety by ensuring you write the fully qualified type name to the XML file, so the same type is automatically loaded.

提交回复
热议问题