I'm trying to build something like the C# type initalizer dynamically:
MyClass class = new MyClass { MyStringProperty= inputString };
I want to build a generic method that reflects over a given type once and returns a delegate which creates a new instance of the class and populates it based on the input parameter. The method signature might look like this:
Func<string,T> CreateFunc<T>();
And calling the resulting function would create a new instance of 'T' with (for example) every public property with of type String to the value of the input string argument.
So assuming that 'MyClass' has only MyStringProperty, the code below would be functionally equivalent to the code at the beginning:
var func = CreateFunc<MyClass>();
func.Invoke(inputString);
I'm pretty familiar with the System.Reflection and System.Linq.Expressions namespaces, and I've done some moderately complex things like this in the past, but this one has me stumped. I want to build a compiled delegate, not simply iterate through the properties using reflection.
Thanks!