2010-11-02 14 views
5

से संपत्ति मूल्य प्राप्त करना मैं सी # में प्रतिबिंब का उपयोग कर रन-टाइम पर क्लास ऑब्जेक्ट से संपत्ति मूल्य तक पहुंचता हूं।सी # - बाल वर्ग

public bool GetValue(string fieldName, out object fieldValue) 
    { 
     // Get type of current record 
     Type curentRecordType = _currentObject.GetType(); 
     PropertyInfo property = curentRecordType.GetProperty(fieldName); 

     if (property != null) 
     { 
      fieldValue = property.GetValue(_currentObject, null).ToString(); 
      return true; 
     } 
     else 
     { 
      fieldValue = null; 
      return false; 
     } 
    } 

मैं प्रॉपर्टी नाम को पैरामीटर के रूप में पास करता हूं: फ़ील्ड नाम इस विधि के लिए। अब, मुझे रन-टाइम पर उपरोक्त कक्षा के बच्चे ऑब्जेक्ट से संपत्ति मूल्य तक पहुंचने की आवश्यकता है।
क्या कोई भी कृपया मार्गदर्शन कर सकता है कि मैं बाल वस्तु संपत्ति मूल्य कैसे प्राप्त कर सकता हूं?

+0

क्या आपको कोई समस्या है या क्या कोई और सवाल है? – leppie

+0

मेरे प्रश्न को संपादित किया गया है, बच्चे ऑब्जेक्ट संपत्ति मूल्य तक पहुंचने के लिए एक तरीका चाहिए। – iniki

उत्तर

6

जब से तुम मनमाने ढंग से-नेस्टेड बच्चे वस्तुओं पर वस्तुओं को खोजने के लिए सक्षम होना चाहते हैं, तो आप एक समारोह है कि आप पुनरावर्ती कॉल कर सकते हैं की जरूरत है। यह इस तथ्य से जटिल है कि आपके बच्चे हो सकते हैं जो उनके माता-पिता को वापस संदर्भित करते हैं, इसलिए आपको अपनी खोज में पहले देखी गई वस्तुओं का ट्रैक रखने की आवश्यकता है।

static bool GetValue(object currentObject, string propName, out object value) 
{ 
    // call helper function that keeps track of which objects we've seen before 
    return GetValue(currentObject, propName, out value, new HashSet<object>()); 
} 

static bool GetValue(object currentObject, string propName, out object value, 
        HashSet<object> searchedObjects) 
{ 
    PropertyInfo propInfo = currentObject.GetType().GetProperty(propName); 
    if (propInfo != null) 
    { 
     value = propInfo.GetValue(currentObject, null); 
     return true; 
    } 
    // search child properties 
    foreach (PropertyInfo propInfo2 in currentObject.GetType().GetProperties()) 
    { // ignore indexed properties 
     if (propInfo2.GetIndexParameters().Length == 0) 
     { 
      object newObject = propInfo2.GetValue(currentObject, null); 
      if (newObject != null && searchedObjects.Add(newObject) && 
       GetValue(newObject, propName, out value, searchedObjects)) 
       return true; 
     } 
    } 
    // property not found here 
    value = null; 
    return false; 
} 

आप जानते हैं कि बच्चे को आपत्ति अपनी संपत्ति में आप इसे करने के लिए पथ पारित कर सकते हैं तो की तरह, यह है:

public bool GetValue(string pathName, out object fieldValue) 
{ 
    object currentObject = _currentObject; 
    string[] fieldNames = pathName.Split("."); 

    foreach (string fieldName in fieldNames) 
    { 
     // Get type of current record 
     Type curentRecordType = currentObject.GetType(); 
     PropertyInfo property = curentRecordType.GetProperty(fieldName); 

     if (property != null) 
     { 
      currentObject = property.GetValue(currentObject, null).ToString(); 
     } 
     else 
     { 
      fieldValue = null; 
      return false; 
     } 
    } 
    fieldValue = currentObject; 
    return true; 
} 

इसके बजाय GetValue("foo", out val) है जैसे कि यह बुलाने की आप इसे GetValue("foo.bar", out val) की तरह कहेंगे।

+0

मेरे मामले में प्रत्येक संपत्ति के लिए पथ नाम देना संभव नहीं है क्योंकि सैकड़ों गुण हैं। एक छोटा और कुशल तरीका चाहते हैं जहां मेरा कोड बाल ऑब्जेक्ट पर जा सकता है और संपत्ति मूल्य प्राप्त कर सकता है केवल माता-पिता ऑब्जेक्ट और प्रॉपर्टी नाम इनपुट के रूप में देता है। – iniki

+0

तो आप कह रहे हैं कि आपके पास ऑब्जेक्ट का पेड़ है जहां सभी वस्तुओं के बीच प्रत्येक संपत्ति का अनोखा नाम होता है, और आप केवल मनमाने ढंग से घोंसला वाली संपत्ति ढूंढने में सक्षम होना चाहते हैं? (यदि आपके पास foo.bar.baz.qux है, तो आप बस "qux" में पास करना चाहते हैं और फ़ंक्शन को यह पता लगाना है कि यह 'foo.bar.baz 'के भीतर है) – Gabe

+0

हां, केवल मुख्य वस्तु और संपत्ति का नाम इनपुट करने की आवश्यकता है – iniki

0

प्रतिबिंब का उपयोग करके बच्चे वस्तु प्राप्त करें, फिर मूल्य को वैसे ही प्राप्त करने के लिए प्रतिबिंब का उपयोग करें।

+0

क्या आपको GetNestedTypes() का उपयोग करके मतलब है? – iniki

0
public void Main(){ 
    var containerObject = new ContainerObject(); 
    object propertyValue; 
    object nestedPropertyValue; 
    if(GetValue(containerObject, "FirstPropertyName", out propertyValue){ 
     bool success = GetValue(propertyValue, "NestedPropertyName", out nestedPropertyValue); 
    } 

} 

public bool GetValue(object currentObject, string propertyName, out object propertyValue) 
{ 
    // Get type of current record 
    var currentObjectType = currentObject.GetType(); 
    PropertyInfo propertyInfo = currentObjectType.GetProperty(propertyName); 

    propertyValue = propertyInfo != null 
        ? propertyInfo.GetValue(currentObject,null) 
        : null; 
    return propertyValue == null; 
} 
+0

धन्यवाद स्मार्टवेवमैन, लेकिन मैं पहले से नहीं हूं कि मुख्य वस्तु की विशेष संपत्ति नेस्टेड संपत्ति के लिए माता-पिता होंगे। इस मामले में, आपका कोड स्निपेट मेरी समस्या का समाधान नहीं करेगा। – iniki

+0

आप GetProperty (नाम) के बजाय GetProperties का उपयोग कर सभी गुण प्राप्त कर सकते हैं। यदि आप उद्देश्य को आगे निर्दिष्ट करेंगे तो मैं आपकी मदद कर सकता हूं। – smartcaveman

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