2010-03-03 18 views
5

पुनः प्राप्त करने मैं निम्नलिखित कोड के माध्यम से संपत्तियों की एक IEnumerable सूची को पुन: प्राप्त कर रहा हूँ:चिंतन: अलग अलग तरीकों से संपत्ति के मूल्य

BindingFlags bindingFlag = BindingFlags.Instance | BindingFlags.Public; 
var dataProperties = typeof(myParentObject).GetProperties(bindingFlag); 

तो मैं सूची के माध्यम से पुनरावृत्ति कर रहा हूँ और प्रत्येक प्रॉपर्टी के मूल्य को पुन: प्राप्त।

मैं यह कर रहा करने के लिए दो अलग-अलग दृष्टिकोण का सामना करना पड़ा, और बस क्या अंतर उन दोनों के बीच है सोचा:

1)

object propertyValue = property.GetGetMethod().Invoke(myObject, null); 

2)

object propertValue = property.GetValue(myObject, null) 

उत्तर

4

वास्तव में , इसमें कोई फर्क नही है। आप Reflector का उपयोग कर getValue के कार्यान्वयन देख सकते हैं:

public override object GetValue(object obj, BindingFlags invokeAttr, 
           Binder binder, object[] index, 
           CultureInfo culture) 
{ 
    MethodInfo getMethod = this.GetGetMethod(true); 
    if (getMethod == null) 
    { 
     throw new ArgumentException(
          Environment.GetResourceString("Arg_GetMethNotFnd")); 
    } 
    return getMethod.Invoke(obj, invokeAttr, binder, index, null); 
} 

वास्तविक प्रकार यहाँ RuntimePropertyInfo है (PropertyInfo एक अमूर्त वर्ग कि getValue के लिए कार्यान्वयन प्रदान नहीं करता है)।

संबंधित मुद्दे