2010-03-29 8 views
19

का उपयोग नेस्टेड वस्तु संपत्ति मूल्य हो रही है मैं दो वर्गों निम्नलिखित है:प्रतिबिंब

var emp1Address = new Address(); 
    emp1Address.AddressLine1 = "Microsoft Corporation"; 
    emp1Address.AddressLine2 = "One Microsoft Way"; 
    emp1Address.City = "Redmond"; 
    emp1Address.State = "WA"; 
    emp1Address.Zip = "98052-6399"; 

    var emp1 = new Employee(); 
    emp1.FirstName = "Bill"; 
    emp1.LastName = "Gates"; 
    emp1.EmployeeAddress = emp1Address; 

मैं एक तरीका है जिसके संपत्ति हो जाता है:

public class Address 
{ 
    public string AddressLine1 { get; set; } 
    public string AddressLine2 { get; set; } 
    public string City { get; set; } 
    public string State { get; set; } 
    public string Zip { get; set; } 
} 

public class Employee 
{ 
    public string FirstName { get; set; } 
    public string MiddleName { get; set; } 
    public string LastName { get; set; } 
    public Address EmployeeAddress { get; set; } 
} 

मैं कर्मचारी वर्ग का एक उदाहरण के रूप में निम्नानुसार है संपत्ति नाम के आधार पर मूल्य निम्नानुसार है:

public object GetPropertyValue(object obj ,string propertyName) 
{ 
    var objType = obj.GetType(); 
    var prop = objType.GetProperty(propertyName); 

    return prop.GetValue(obj, null); 
} 

उपर्युक्त विधि कॉल के लिए ठीक काम करती है GetPropertyValue(emp1, "FirstName") की तरह, लेकिन अगर मैं GetPropertyValue(emp1, "Address.AddressLine1") आज़माता हूं तो यह अपवाद फेंकता है क्योंकि objType.GetProperty(propertyName); नेस्टेड ऑब्जेक्ट प्रॉपर्टी वैल्यू का पता लगाने में सक्षम नहीं है। क्या इसे ठीक करने का कोई तरीका है?

उत्तर

10
var address = GetPropertyValue(GetPropertyValue(emp1, "Address"), "AddressLine1"); 

वस्तु कर्मचारी "Address.AddressLine1" नामक एक एकल संपत्ति नहीं है, यह एक संपत्ति "पता" नामित किया गया है, जो अपने आप में एक संपत्ति "AddressLine1" नामक है।

16
public object GetPropertyValue(object obj, string propertyName) 
{ 
    foreach (var prop in propertyName.Split('.').Select(s => obj.GetType().GetProperty(s))) 
     obj = prop.GetValue(obj, null); 

    return obj; 
} 

धन्यवाद, मैं यहां एक ही समस्या का उत्तर ढूंढ रहा हूं। मैंने नेस्टेड गुणों का समर्थन करने के लिए अपनी मूल विधि को संशोधित करना समाप्त कर दिया। यह नेस्टेड विधि कॉल करने से अधिक मजबूत होना चाहिए जो 2 से अधिक घोंसले के स्तर के लिए बोझिल हो सकता है।

+0

क्या यह अन्य उत्तर का उत्तर है? –

+2

यह स्तर 2 गुणों के लिए काम नहीं करता – SyntaxGoonoo

+0

एक आकर्षण की तरह काम किया! –

2

जाओ नेस्ट गुण जैसे, Developer.Project.Name

private static System.Reflection.PropertyInfo GetProperty(object t, string PropertName) 
      { 
       if (t.GetType().GetProperties().Count(p => p.Name == PropertName.Split('.')[0]) == 0) 
        throw new ArgumentNullException(string.Format("Property {0}, is not exists in object {1}", PropertName, t.ToString())); 
       if (PropertName.Split('.').Length == 1) 
        return t.GetType().GetProperty(PropertName); 
       else 
        return GetProperty(t.GetType().GetProperty(PropertName.Split('.')[0]).GetValue(t, null), PropertName.Split('.')[1]); 
      } 
0

ऊपर का एक संशोधित संस्करण बहुस्तरीय नेस्टेड गुण

private static System.Reflection.PropertyInfo GetProperty(object t, string PropertName, out object Value) 
     { 
      Value = ""; 
      var v = t.GetType().GetProperties(); 
      if (t.GetType().GetProperties().Count(p => p.Name == PropertName.Split('.')[0]) == 0) 
       //throw new ArgumentNullException(string.Format("Property {0}, is not exists in object {1}", PropertName, t.ToString())); 
       return null; 
      if (PropertName.Split('.').Length == 1) 
      { 
       var Value1 = t.GetType().GetProperty(PropertName).GetValue(t, null); 
       Value = Value1;//.ToString(); 
       return t.GetType().GetProperty(PropertName); 
      } 
      else 
      { 
       //return GetProperty(t.GetType().GetProperty(PropertName.Split('.')[0]).GetValue(t, null), PropertName.Split('.')[1], out Value); 
       return GetProperty(t.GetType().GetProperty(PropertName.Split('.')[0]).GetValue(t, null), PropertName.Substring(PropertName.IndexOf('.') + 1, PropertName.Length - PropertName.IndexOf('.') - 1), out Value); 
      } 
     } 
0

यह स्तर 1 के लिए काम करेंगे पाने के लिए और स्तर 2 वस्तु गुण उदाहरण के लिए Firstname और Address.AddressLine1

public object GetPropertyValue(object obj, string propertyName) 
{ 
    object targetObject = obj; 
    string targetPropertyName = propertyName; 

    if (propertyName.Contains('.')) 
    { 
     string[] split = propertyName.Split('.'); 
     targetObject = obj.GetType().GetProperty(split[0]).GetValue(obj, null); 
     targetPropertyName = split[1]; 
    } 

    return targetObject.GetType().GetProperty(targetPropertyName).GetValue(targetObject, null); 
} 
5

यह नेस्टेड संपत्ति की असीमित संख्या के लिए काम करेंगे।

public object GetPropertyValue(object obj, string propertyName) 
{ 
    var _propertyNames = propertyName.Split('.'); 

    for (var i = 0; i < _propertyNames.Length; i++) 
    { 
     if (obj != null) 
     { 
      var _propertyInfo = obj.GetType().GetProperty(_propertyNames[i]); 
      if (_propertyInfo != null) 
       obj = _propertyInfo.GetValue(obj); 
      else 
       obj = null; 
     } 
    } 

    return obj; 
} 

उपयोग:

GetPropertyValue(_employee, "Firstname"); 
GetPropertyValue(_employee, "Address.State"); 
GetPropertyValue(_employee, "Address.Country.Name"); 
7

मैं इस विधि का उपयोग (नेस्टेड संपत्ति की असीमित संख्या) गुण से मान प्राप्त करने के लिए नीचे के रूप में:

"संपत्ति"

"पता। स्ट्रीट"

"पता। देश।नाम "

public static object GetPropertyValue(object src, string propName) 
    { 
     if (src == null) throw new ArgumentException("Value cannot be null.", "src"); 
     if (propName == null) throw new ArgumentException("Value cannot be null.", "propName"); 

     if(propName.Contains("."))//complex type nested 
     { 
      var temp = propName.Split(new char[] { '.' }, 2); 
      return GetPropertyValue(GetPropertyValue(src, temp[0]), temp[1]); 
     } 
     else 
     { 
      var prop = src.GetType().GetProperty(propName); 
      return prop != null ? prop.GetValue(src, null) : null; 
     } 
    } 

यहाँ फिडल:।

public static class TypeExtensions 
{ 
    public static PropertyInfo GetSubProperty(this Type type, string treeProperty, object givenValue) 
    { 
     var properties = treeProperty.Split('.'); 
     var value = givenValue; 

     foreach (var property in properties.Take(properties.Length - 1)) 
     { 
      value = value.GetType().GetProperty(property).GetValue(value); 

      if (value == null) 
      { 
       return null; 
      } 
     } 

     return value.GetType().GetProperty(properties[properties.Length - 1]); 
    } 

    public static object GetSubPropertyValue(this Type type, string treeProperty, object givenValue) 
    { 
     var properties = treeProperty.Split('.'); 
     return properties.Aggregate(givenValue, (current, property) => current.GetType().GetProperty(property).GetValue(current)); 
    } 
} 
0

मैं इस प्रस्ताव के लिए प्रकार पर एक विस्तार विधि बनाया मनमाने ढंग से गहरे गुण, शून्य मूल्यों और अमान्य गुणों को संभालते हैं:

public static object GetPropertyVal(this object obj, string name) { 
    if (obj == null) 
     return null; 

    var parts = name.Split(new[] { '.' }, 2); 
    var prop = obj.GetType().GetProperty(parts[0]); 
    if (prop == null) 
     throw new ArgumentException($"{parts[0]} is not a property of {obj.GetType().FullName}."); 

    var val = prop.GetValue(obj); 
    return (parts.Length == 1) ? val : val.GetPropertyVal(parts[1]); 
} 
1

फिर भी एक और अधिक अंतर बाहर निकालने के लिए लघु & मिठाई, का समर्थन करता है: https://dotnetfiddle.net/PvKRH0