I understand that Delegates offer high performance reflection maybe just 15% slower than regular explicit c# code. However all the examples I can find on stackoverflow are based on prior knowledge of the type of a method/property being accessed via a delegate.
Given such prior knowledge of a class, why resort to reflected Delegate access in the first place?
Anyhow the reflection coding task I face is how to implement high performance property get/set access for an unknown list of class properties where just a class type name is supplied at runtime? I can code the basics of reflection inspection to produce a list of properties but how do I wire up a set of Delegate based accessors for a potentially random set of property types?
Assuming the property types are limited to a range of basic DB column types is the answer a case statement that returns a:
Func<int> or Func<string> etc?
Edit-1: I am limited to .Net 3.5
This solution uses expression trees since they’re fairly easy to compose, and they provide the handy Compile() method to get an actual Delegate upon which you can invoke. I made the Func actually take in the object (so Func<T, TResult> rather than just Func<TResult>) so you can obtain the property value from any instance.
Edit: Added setter implementation as well.
Prints out “Hello” by first using the dynamic setter to set it to “Hello” and then using the dynamic getter to obtain the property from the object.