title: Setting Values on a derived class from the base class constructor using Reflection
body: <p>I have two classes like this:</p>
  
  <pre><code>public abstract class MyBase
  {
       protected MyBase(){
           Initialize();
       }
  
       protected IDictionary&lt;string,string&gt; _data;
  
       private void Initialize() {
           // Use Reflection to get all properties
           // of the derived class (e.g., call new MyDerived() then
           // I want to know the names "Hello" and "ID" here
           var data = GetDataFromBackend(propertyNamesFromDerived);
           _data = data;
       }
  }
  
  public class MyConcrete : MyBase
  {
       public MyConcrete(){
           // Possibly use Reflection here
           Hello = _data["Hello"];
           ID = new Guid(data["ID"]);
       }
  
       public string Hello {get;set;}
       public Guid ID {get; set;}
  }
  </code></pre>
  
  <p>As you see, I want the constructor of my base class to know about the properties of the derived class I'm instantiating.</p>
  
  <p>Now, this seems like a huge and big code smell, so let me give some more background about my intentions, maybe there is a better way.</p>
  
  <p>I have a backend system that stores Key/Value Pairs, essentially a <code>Dictionary&lt;string,string&gt;</code>. I want to abstract away working with this backend system in a way where people can create classes whose properties are Keys into the backend system. When they construct this object, it will automatically load the data from that system and initialize all the variables to it.</p>
  
  <p>In other words, I've just reinvented serialization, except that I don't control the backend system and just rather make working with it really painless. I don't want callers to have to call <code>Initialize()</code> after constructing the object, because in 100% of the cases you have to initalize it after constructing.</p>
  
  <p>I don't want to move the initialize code into the Derived Classes, except for string-to-business-object conversion.</p>
  
  <p>Would I have to use a Factory? Or is it considered safe to look at the property <em>names</em> of a derived class in a base constructor? (Don't care about their values and that they aren't initialized, just need the names).</p>
  
  <p>Or is there a better way altogether to provide a facade between a Dictionary of strings and a concrete business object?</p>
  
  <p><em>Edit:</em> This is .net 3.5, so no System.Dynamic which would make this trivial :(</p>
  
  <p><strong>Edit 2:</strong> After looking at the Answers and thinking through this some more, I guess my question really boils down to this now: Is calling GetType().GetProperties() from a base constructor in order to get the <em>Names</em> of Properties and if they are decorated with a certain Attribute safe?</p>