Best practice instantiating generic delegates and accessing property getters I want to create delegates to access properties of different objects without knowing them in advance.

I have the following definitions

public delegate T MyMethod&lt;K, T&gt;(K data);

public static MyMethod&lt;K, T&gt; CreatePropertyGetter&lt;K, T&gt;(PropertyInfo property)
{       
   MethodInfo mi = property.DeclaringType.GetMethod("get_" + property.Name);        
   return (MyMethod&lt;K, T&gt;)Delegate.CreateDelegate(typeof(MyMethod&lt;K, T&gt;), mi);
}


where T can be decimal, string, datetime or int

I have some initializing code that will create MyMethod delegates, based on the reflected properties of my object as follows:

foreach (PropertyInfo property in entityType.GetProperties())
{               
    switch (property.PropertyType.Name)
    {
        case "System.Decimal":
            return CreatePropertyGetter&lt;T, decimal&gt;(property);
        case "System.DateTime":
            return CreatePropertyGetter&lt;T, DateTime&gt;(property);
        case "System.String":
            return CreatePropertyGetter&lt;T, DateTime&gt;(property);
    }
}


Is there a better way to


create property getters?
enumerate through the supported property types hard-coded as strings?


However, performance is an issue, since these delegates will be called frequently (ticking scenario), so any casting will slow it down. While a more elegant solution is desirable, performance is still my main concern
As my answer on Stack Overflow states, there is a solution which allows you to create a getter delegate for any type, so the entire switch block can be dropped. Personally, I find that the cleanest solution. The only overhead of this approach is the cast from object to the specific type where necessary. (For a getter this would happen only once on the return value.) I'm guessing if you would benchmark this, the result would be negligible.

When you are certain that the only types required are those specified, or that no new types are expected very often, you could still opt to use your current approach. I would apply a few changes however:


Use PropertyInfo.GetGetMethod() to retrieve your MethodInfo for the getter.
Use the type of PropertyType directly to determine which CreatePropertyGetter to call instead of the name of the type.
Use Func&lt;K, T&gt; instead of MyMethod.
Use a Dictionary holding anonymous functions which create the correct delegate for the specified type of the key. This can replace the switch statement.


The last point is possible as follows:

Dictionary&lt;Type, Func&lt;PropertyInfo, object&gt;&gt; getterCreators =
    new Dictionary&lt;Type, Func&lt;PropertyInfo, object&gt;&gt;
    {
        { typeof(string), p =&gt; CreatePropertyGetter&lt;T, string&gt;(p) },
        { typeof(DateTime), p =&gt; CreatePropertyGetter&lt;T, DateTime&gt;(p) },
        { typeof(decimal), p =&gt; CreatePropertyGetter&lt;T, decimal&gt;(p) }
    };


Ofcourse you still have to cast from object to the desired type.

UPDATE:

As Timwi pointed out in the comments, p =&gt; CreatePropertyGetter&lt;T, ...&gt;(p) can be further reduced to: CreatePropertyGetter&lt;T, ...&gt;.