2012-08-06 17 views
11

क्या ऐसा करने का कोई तरीका है? मैं परीक्षण करने का प्रयास करता हूं कि किसी ऑब्जेक्ट की कोई संपत्ति मौजूद है और यदि ऐसा होता है, तो मैं इसके लिए एक मान निर्धारित करना चाहता हूं। (हो सकता है कि पूरा विचार बुरा है, अगर सच है? - क्यों)सी # वैरिएबल नाम से संपत्ति प्राप्त करें और सेट करें

class Info 
{ 
    public string X1{ set; get; } 
    public string X2{ set; get; } 
    public string X3{ set; get; } 
} 

Dictionary<string, string> values = new Dictionary<string, string>(); 
values.Add("X1","blah1"); 
values.Add("X2","blah2"); 
values.Add("NotThere","blah3"); 

Info info = new Info(); 

foreach (var item in values) 
{ 
    string propertyName = item.Key; 
    string value = item.Value; 
    if (info.GetType().GetProperty(propertyName) != null) //this probably works 
    { 
     info.propertyName = value; //this doesn't, how to set it? 
    } 
} 
+0

आप इस के बहुत से कर रहे हैं, तो आप इस तरह के [FastMember] के रूप में एक अनुकूलित उपकरण का उपयोग कर सकते हैं (http://nuget.org/संकुल/FastMember) –

उत्तर

28

हां, आपके PropertyInfo.SetValue विधि के लिए उदाहरण के लिए की तलाश में

var propInfo = info.GetType().GetProperty(propertyName); 
if (propInfo != null) 
{ 
    propInfo.SetValue(info, value, null); 
} 
3

आप संपत्ति पर SetValue विधि को लागू करने की जरूरत है:

var property = info.GetType().GetProperty(propertyName); 
if (property != null) 
{ 
    property.SetValue(info, value, null); 
} 
6
var propertyInfo = info.GetType().GetProperty(propertyName); 
if (propertyInfo != null) //this probably works. Yes it is 
    { 
     propertyInfo.SetValue(info, value, null); 
    } 
1

मैं प्रतिबिंब हर बार थोड़ा धीमा है का उपयोग कर लगता है, इसलिए, अगर आपको लगता है कि प्रारंभ आप एक बार से अधिक करना अभिव्यक्ति वृक्ष का उपयोग कर सकते हैं। लेकिन हर बार आपके शब्दकोश में गुणों के समान क्रम होना चाहिए।

संभव कोड

class Info 
{ 
    public string X1 { set; get; } 
    public string X2 { set; get; } 
    public int X3 { set; get; } 
    private Action<Info, List<object>> initAction; 

    public void Init(Dictionary<string, object> 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<object>)); 
      ParameterExpression value = Expression.Variable(typeof(object)); 
      ParameterExpression enumerator = Expression.Variable(typeof(IEnumerator)); 

      var expList = new List<Expression>(); 
      expList.Add(Expression.Assign(enumerator, Expression.TypeAs(Expression.Call(valuesExpression, "GetEnumerator", null),typeof(IEnumerator)))); 
      foreach (var initRecord in initDict) 
      { 
       Expression moveNextExp = Expression.Call(enumerator, "MoveNext", null); 
       expList.Add(moveNextExp); 
       Type type = initRecord.Value.GetType(); 
       expList.Add(Expression.Assign(value, Expression.PropertyOrField(enumerator, "Current"))); 
       Expression assignExp = GetPropAssigner(initRecord.Key, type, targetInstanceExpression, value); 
       expList.Add(assignExp); 
      } 
      Expression block = Expression.Block 
      (
       new[] { value, enumerator }, 
       expList 
      ); 
      //compile epression tree and get init action 
      initAction = Expression.Lambda<Action<Info, List<object>>>(block, targetInstanceExpression, valuesExpression).Compile(); 
     } 
     initAction(this, initDict.Values.ToList()); 
    } 
    //little method to create property assigner 
    public static Expression GetPropAssigner(string propName, Type type, 
     ParameterExpression targetInstanceExp, ParameterExpression valueExp) 
    { 
     MemberExpression fieldExp = Expression.PropertyOrField(targetInstanceExp, propName); 
     BinaryExpression assignExp = Expression.Assign(fieldExp, type.IsValueType ? Expression.Unbox(valueExp, type) : Expression.TypeAs(valueExp, type)); 
     return assignExp; 
    } 
} 

उपयोग:

var values = new Dictionary<string, object>(); 
      values.Add("X1", "blah1"); 
      values.Add("X2", "blah2"); 
      values.Add("X3", 8); 


Info info = new Info(); 
info.Init(values); 
संबंधित मुद्दे