title: How to infer the type of a derived class in base class?
body: <p>I want to create a method that allows me to change arbitrary properties of classes that derive from my base class, the result should look like this: <code>SetPropertyValue("size.height", 50);</code> – where <code>size</code> is a property of my derived class and <code>height</code> is a property of <code>size</code>.</p>
  
  <p>I'm almost done with my implementation but there's one final obstacle that I want to solve before moving on, to describe this I will first have to explain my implementation a bit:</p>
  
  <ul>
  <li>Properties that can be modified are decorated with an attribute</li>
  <li>There's a method in my base class that searches for all derived classes and their decorated properties</li>
  <li>For each property I generate a "property modifier", a class that contains 2 delegates: one to set and one to get the value of the property.</li>
  <li>Property Modifiers are stored in a dictionary, with the name of the property as key</li>
  <li>In my base class, there is another dictionary that contains all property-modifier-dictionaries, with the Type of the respective class as key.</li>
  </ul>
  
  <p>What the <code>SetPropertyValue</code> method does is this:</p>
  
  <ul>
  <li>Get the correct property-modifier-dictionary, using the concrete type of the derived class (&lt;- yet to solve)</li>
  <li>Get the property modifier of the property to change (e.g. of the property <code>size</code>)</li>
  <li>Use the get or set delegate to modify the property's value</li>
  </ul>
  
  <p>Some example code to clarify further:</p>
  
  <pre><code>private static Dictionary&lt;RuntimeTypeHandle, object&gt; EditableTypes; //property-modifier-dictionary
  
  protected void SetPropertyValue&lt;T&gt;(EditablePropertyMap&lt;T&gt; map, string property, object value) {
    var property = map[property]; // get the property modifier
    property.Set((T)this, value); // use the set delegate (encapsulated in a method)
  }
  </code></pre>
  
  <p>In the above code, <code>T</code> is the Type of the actual (derived) class. I need this type for the get/set delegates. The problem is how to get the <code>EditablePropertyMap&lt;T&gt;</code> when I don't know what T is.</p>
  
  <p>My current (ugly) solution is to pass the map in an overriden virtual method in the derived class:</p>
  
  <pre><code>public override void SetPropertyValue(string property, object value) {
    base.SetPropertyValue((EditablePropertyMap&lt;ExampleType&gt;)EditableTypes[typeof(ExampleType)], property, value);
  }
  </code></pre>
  
  <p>What this does is: get the correct dictionary containing the property modifiers of this class using the class's type, cast it to the appropiate type and pass it to the <code>SetPropertyValue</code> method. </p>
  
  <p>I want to get rid of the <code>SetPropertyValue</code> method in my derived class (since there are a lot of derived classes), but don't know yet how to accomplish that. I cannot just make a virtual <code>GetEditablePropertyMap&lt;T&gt;</code> method because I cannot infer a concrete type for T then. I also cannot acces my dictionary directly with a type and retrieve an <code>EditablePropertyMap&lt;T&gt;</code> from it because I cannot cast to it from <code>object</code> in the base class, since again I do not know <code>T</code>.</p>
  
  <p>I found some neat tricks to infere types (e.g. by adding a dummy <code>T</code> parameter), but cannot apply them to my specific problem. I'd highly appreciate any suggestions you may have for me.</p>
  
  <p>EDIT:
  Although this is already a wall of text, I have to add some more notes:</p>
  
  <ul>
  <li>Performance is important. I can (and do) use reflection <em>once</em> on startup to build my dictionary and the delegates, but during runtime it's not acceptable (unless you can convince me that it is as fast as a delegate/method call ;)</li>
  <li>I store the <code>EditablePropertyMap</code>s as objects because I found no other way (different generic type parameters) – perhaps this can be refined as well.</li>
  <li>I cannot use the Curiously recurring template pattern as Mehrdad suggested. My base class cannot be generic for other reasons (has nothing to do with the problem at hand).</li>
  </ul>
  
  <p>To clarify what <code>EditableProperty</code> / <code>EditablePropertyMap</code> does:</p>
  
  <pre><code>public class EditableProperty&lt;T&gt; {
    private Func&lt;T, object&gt; getter;
    private Action&lt;T, object&gt; setter;
    ...
    public object Get(T obj) {  return getter(obj); }
    public void Set(T obj, object value) { setter(obj, value);}
  </code></pre>
  
  <p>The map is a mere wrapper for a <code>Dictionary&lt;string, EditableProperty&lt;T&gt;&gt;</code>.</p>