I think you can use DynamicObject, but it would be a little bit different than what you're wanting. I am not sure that what you're wanting to do is possible in C# (definitely possible in other languages though). This is a pseudocode attempt at something (maybe not the most amazing piece of code)
public class MyDynamicObject<T> : DynamicObject {
private T referenceObject;
public MyDynamicObject<T>() {
this.referenceObject = new T();
}
public override bool TrySetMember(SetMemberBinder binder, Object value) {
var propertyName = // get from binder parameter
typeof(T).SetValue(referenceObject, value);
}
public T Compile() {
return referenceObject;
}
}
Then I believe you can do something like:
var propertyName = "Name";
var dynamicObject = new MyDynamicObject<MyTestClass>();
dynamicObject[propertyName] = "Foo";
var myTestClass = dynamicObject.Compile();
Overall, I would say that you should investigate the DynamicObject functionality. The function that I overrode may not be the correct one for an array (I always forget which function to override). But you should give it a shot. If you want to see a really sweet project that uses DynamicObjects in the best way possible, check out https://github.com/markrendle/Simple.Data
MyTestClassdoesn't have a property calledpropNameHow'll you do that using reflection? Can you show us?