Is there any way to do this? I try to test if a property of an object exists and if it does, I want to set a value to it. (Maybe the complete idea is bad, if true - why?)
I think using reflection each time is a bit slow, so, if you do that initialization more than once you can use Expression Tree. But each time your dictionary should have same order of properties to init.
Possible code
class Info
{
public string X1 { set; get; }
public string X2 { set; get; }
public int X3 { set; get; }
private Action> initAction;
public void Init(Dictionary initDict)
{
//on first usage we deal with reflection and build expression tree to init properties
if (initAction==null)
{
ParameterExpression targetInstanceExpression = Expression.Parameter(this.GetType());
ParameterExpression valuesExpression = Expression.Parameter(typeof(List
Usage:
var values = new Dictionary();
values.Add("X1", "blah1");
values.Add("X2", "blah2");
values.Add("X3", 8);
Info info = new Info();
info.Init(values);