title: How to use a configuration file to generate and initialize objects at runtime?
body: <p>I am currently working on a project where I need to create objects at runtime based on a configuration file.</p>
  
  <p><strong>Project example</strong></p>
  
  <p>Here is a simplified example of the project I am working on.</p>
  
  <p>Every object created must implement a specific interface, let it be called the <code>IObjectInterface</code> for the example purpose :</p>
  
  <pre><code>public interface IObjectInterface
  {
      void DoSomething();
  }
  </code></pre>
  
  <p>Let's assume that I have several classes that implement this interface, each having a specific implementation, and multiple other properties relevant to the specific type :</p>
  
  <pre><code>public class SimpleObject : IObjectInterface
  {
      public void DoSomething()
      {
          Console.WriteLine("SimpleObject did something");
      }
  }
  
  public class ComplexObject : IObjectInterface
  {
      public string ObjectName { get; set; }
  
      public void DoSomething()
      {
          Console.WriteLine("The ComplexObject named {0} did something", this.ObjectName);
      }
  }
  
  public class VeryComplexObject : IObjectInterface
  {
      public string ObjectName { get; set; }
      public bool CanDoSomething { get; set; }
  
      public void DoSomething()
      {
          if (this.CanDoSomething)
          {
              Console.WriteLine("The ComplexObject named {0} did something", this.ObjectName);
          }
          else
          {
              Console.WriteLine("The ComplexObject named {0} tried to do something, but was not allowed to", this.ObjectName);
          }
      }
  }
  </code></pre>
  
  <p>I need to be able to create a <code>IObjectInterface</code> object corresponding to a specific Id using the ObjectBuilder class :</p>
  
  <pre><code>public class ObjectBuilder
  {
      IObjectInterface BuildObject(string objectId)
      {
          // Build object here based on provided Id
      }
  }
  </code></pre>
  
  <p>What I am looking for is a way to specify, in a configuration file, the correct implementation of <code>IObjectInterface</code> that should be created, and the parameter values associated to the specific type.
  The ideal configuration file should look like this :</p>
  
  <pre><code>&lt;objects&gt;
   &lt;object id="Simple"&gt;
    &lt;objectInterface type="SimpleObject" /&gt;
   &lt;/object&gt;
   &lt;object id="Complex"&gt;
    &lt;objectInterface type="ComplexObject"&gt;
     &lt;ObjectName value="MyObject" /&gt;
    &lt;/objectInterface&gt;
   &lt;/object&gt;
  &lt;/objects&gt;
  </code></pre>
  
  <p>I think I should be able to manage the object instantiation part, but I don't really see how I can manage the initialization of the instances with clean code.</p>
  
  <ul>
  <li>Do you have some leads on how I can implement such a configuration file ?</li>
  <li>How can I easily create the final instances of the needed objects and initialize them with the parameters provided in the configuration file ?</li>
  <li>Are there any frameworks that could help me implementing a solution to my problem ?</li>
  </ul>