<p>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 classes go into the collection upon starting the program, taking something that looks like :</p>

<pre><code>&lt;class1 prop1="foo" prop2="bar"/&gt;
</code></pre>

<p>and turning that into :</p>

<pre><code>blah = new class1();
blah.prop1="foo";
blah.prop2="bar";
</code></pre>

<p>In a very generic way.  The thing I don't know how to do is take the string <code>prop1</code> in the config file and turn that into the actual property accessor in the code.  Are there any meta-programming facilities in C# to allow that?</p>

[{'answer_id': 31244, 'author': 'Darren Kopp', 'author_id': 77, 'author_profile': 'https://Stackoverflow.com/users/77', 'pm_score': 0, 'selected': False, 'text': "<p>Reflection is what you want. Reflection + TypeConverter. Don't have much more time to explain, but just google those, and you should be well on your way. Or you could just use the xml serializer, but then you have to adhere to a format, but works great.</p>\n"}, {'answer_id': 31246, 'author': 'Rob Cooper', 'author_id': 832, 'author_profile': 'https://Stackoverflow.com/users/832', 'pm_score': 3, 'selected': False, 'text': '<p>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..</p>\n<p><a href="http://www.diranieh.com/NETSerialization/XMLSerialization.htm" rel="noreferrer">This is a pretty good article on serialization</a></p>\n<h2>Edit</h2>\n<p>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.</p>\n<p>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.</p>\n'}, {'answer_id': 31252, 'author': 'Frank Krueger', 'author_id': 338, 'author_profile': 'https://Stackoverflow.com/users/338', 'pm_score': 2, 'selected': False, 'text': '<p>Plenty of metaprogramming facilities.</p>\n\n<p>Specifically, you can get a reference to the assembly that holds these classes, then easily get the <code>Type</code> of a class from its name. See <a href="http://msdn.microsoft.com/en-us/library/y0cd10tb.aspx" rel="nofollow noreferrer">Assembly.GetType Method (String)</a>.</p>\n\n<p>From there, you can instantiate the class using <code>Activator</code> or the constructor of the <code>Type</code> itself. See <a href="http://msdn.microsoft.com/en-us/library/system.activator.createinstance.aspx" rel="nofollow noreferrer">Activator.CreateInstance Method</a>.</p>\n\n<p>Once you have an instance, you can set properties by again using the <code>Type</code> object. See <a href="http://msdn.microsoft.com/en-us/library/system.type.getproperty.aspx" rel="nofollow noreferrer">Type.GetProperty Method</a> and/or <a href="http://msdn.microsoft.com/en-us/library/system.type.getfield.aspx" rel="nofollow noreferrer">Type.GetField Method</a> along <a href="http://msdn.microsoft.com/en-us/library/system.reflection.propertyinfo.setvalue.aspx" rel="nofollow noreferrer">PropertyInfo.SetValue Method</a>.</p>\n'}, {'answer_id': 31258, 'author': 'Markus Olsson', 'author_id': 2114, 'author_profile': 'https://Stackoverflow.com/users/2114', 'pm_score': 3, 'selected': False, 'text': '<p>Reflection or XML-serialization is what you\'re looking for.</p>\n\n<p>Using reflection you could look up the type using something like this</p>\n\n<pre><code>public IYourInterface GetClass(string className)\n{\n    foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies()) \n    {            \n        foreach (Type type in asm.GetTypes())\n        {\n            if (type.Name == className)\n                return Activator.CreateInstance(type) as IYourInterface;\n        }   \n    }\n\n    return null;\n}\n</code></pre>\n\n<p>Note that this will go through all assemblies. You might want to reduce it to only include the currently executing assembly.</p>\n\n<p>For assigning property values you also use reflection. Something along the lines of</p>\n\n<pre><code>IYourInterface o = GetClass("class1");\no.GetType().GetProperty("prop1").SetValue(o, "foo", null);\n</code></pre>\n\n<p>While reflection might be the most flexible solution you should also take a look at <a href="http://www.devhood.com/Tutorials/tutorial_details.aspx?tutorial_id=236" rel="noreferrer">XML-serialization</a> in order to skip doing the heavy lifting yourself.</p>\n'}, {'answer_id': 31261, 'author': 'Lars Truijens', 'author_id': 1242, 'author_profile': 'https://Stackoverflow.com/users/1242', 'pm_score': 3, 'selected': False, 'text': '<p><a href="http://msdn.microsoft.com/en-us/library/ms173183.aspx" rel="noreferrer">Reflection</a> allows you to do that. You also may want to look at <a href="http://msdn.microsoft.com/en-us/library/ms950721.aspx" rel="noreferrer">XML Serialization</a>.</p>\n\n<pre><code>Type type = blah.GetType();\nPropertyInfo prop = type.GetProperty("prop1");\nprop.SetValue(blah, "foo", null);\n</code></pre>\n'}, {'answer_id': 31488, 'author': 'Brian Ensink', 'author_id': 1254, 'author_profile': 'https://Stackoverflow.com/users/1254', 'pm_score': 3, 'selected': False, 'text': '<p>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 <code>Things</code> 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 <code>Thing</code> was an interface.</p>\n\n<pre><code>using System;\nusing System.Collections.Generic;\nusing System.Text;\nusing System.Xml.Serialization;\nusing System.IO;\n\nnamespace ConsoleApplication1\n{\n    class Program\n    {\n        static void Main()\n        {\n            string xml =\n                "&lt;?xml version=\\"1.0\\"?&gt;" + \n                "&lt;config&gt;" +\n                "&lt;stuff&gt;" + \n                "  &lt;class1 prop1=\\"foo\\" prop2=\\"bar\\"&gt;&lt;/class1&gt;" +\n                "  &lt;class2 prop1=\\"FOO\\" prop2=\\"BAR\\" prop3=\\"42\\"&gt;&lt;/class2&gt;" +\n                "&lt;/stuff&gt;" +\n                "&lt;/config&gt;";\n            StringReader sr = new StringReader(xml);\n            XmlSerializer xs = new XmlSerializer(typeof(ThingCollection));\n            ThingCollection tc = (ThingCollection)xs.Deserialize(sr);\n\n            foreach (Thing t in tc.Things)\n            {\n                Console.WriteLine(t.ToString());\n            }\n        }\n    }\n\n    public abstract class Thing\n    {\n    }\n\n    [XmlType(TypeName="class1")]\n    public class SomeThing : Thing\n    {\n        private string pn1;\n        private string pn2;\n\n        public SomeThing()\n        {\n        }\n\n        [XmlAttribute("prop1")]\n        public string PropertyNumber1\n        {\n            get { return pn1; }\n            set { pn1 = value; }\n        }\n\n        [XmlAttribute("prop2")]\n        public string AnotherProperty\n        {\n            get { return pn2; }\n            set { pn2 = value; }\n        }\n    }\n\n    [XmlType(TypeName="class2")]\n    public class SomeThingElse : SomeThing\n    {\n        private int answer;\n\n        public SomeThingElse()\n        {\n        }\n\n        [XmlAttribute("prop3")]\n        public int TheAnswer\n        {\n            get { return answer; }\n            set { answer =value; }\n        }\n    }\n\n    [XmlType(TypeName = "config")]\n    public class ThingCollection\n    {\n        private List&lt;Thing&gt; things;\n\n        public ThingCollection()\n        {\n            Things = new List&lt;Thing&gt;();\n        }\n\n        [XmlArray("stuff")]\n        [XmlArrayItem(typeof(SomeThing))]\n        [XmlArrayItem(typeof(SomeThingElse))]\n        public List&lt;Thing&gt; Things\n        {\n            get { return things; }\n            set { things = value; }\n        }\n    }\n}\n</code></pre>\n'}, {'answer_id': 31493, 'author': 'FlySwat', 'author_id': 1965, 'author_profile': 'https://Stackoverflow.com/users/1965', 'pm_score': 1, 'selected': False, 'text': '<p>I recently did something very similar, I used an abstract factory. In fact, you can see the basic concept here:</p>\n\n<p><a href="https://stackoverflow.com/questions/27294/abstract-factory-design-pattern">Abstract Factory Design Pattern</a></p>\n'}, {'answer_id': 12529710, 'author': 'Dima', 'author_id': 883741, 'author_profile': 'https://Stackoverflow.com/users/883741', 'pm_score': 1, 'selected': False, 'text': '<p>I think you can utilize Dynamics here. Create ExpandoObject, it can be used either as Dictionary for setting properties from xml config.</p>\n'}]
['https://Stackoverflow.com/questions/31238', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/634/']