2016-06-15 5 views
2

Ich versuche eine Möglichkeit zu finden, ein Objekt durchzulaufen und zu durchlaufen, um alle seine Eigenschaften (Name und Wert) eines Objekts zu erhalten. Ich kann erfolgreich durch die einfachen Eigenschaften (wie Saiten, int, etc .., iterieren aber wenn es hat eine Eigenschaft, die Eigenschaften enthält - das ist, wo das Problem ist ...C# Schleife/Iterieren durch Objekt, um Eigenschaftswerte mit komplexen Eigenschaftstypen zu erhalten

[ Working for Simple string/int/bool properties ], but I need something that will work with nested/complex property types. 

      foreach (PropertyInfo spotProperties in spot.GetType().GetProperties()) 
      { 
       // Simple property type (string, int, etc...) add the property and its value to the node. 
       var attributeName = spotProperties.Name; 
       resultElement.Add(new XElement(attributeName, spotProperties.GetValue(spot, null))); 
      } 

Beispielcode von dem, was ich zu erreichen versuchte, aber bekommen konnte nicht // kann nicht zur Arbeit kommen Schleife durch komplexe Immobilienart zu arbeiten.

  foreach (PropertyInfo spotProperties in spot.GetType().GetProperties()) 
      { 
       if (--spotProperties is complex type then --) 
       { 
        // The item is a complex data type, and needs to have it's properties iterated and added to the node. 
        foreach (PropertyInfo childSpotProperty in spotProperties.GetValue(spot, null).GetType().GetProperties()) 
        { 
         var attributeName = ((DisplayNameAttribute)childSpotProperty.GetCustomAttributes(typeof(DisplayNameAttribute), false).FirstOrDefault() as DisplayNameAttribute)?.DisplayName ?? childSpotProperty.Name; 
         //resultElement.Add(new XElement(attributeName, childSpotProperty.GetValue(childSpotProperty, null))); 
        } 
       } 
       else 
       { 
        // Simple property type (string, int, etc...) add the property and its value to the node. 
        var attributeName = spotProperties.Name; 
        resultElement.Add(new XElement(attributeName, spotProperties.GetValue(spot, null 
       } 
      } 

Bitte lassen Sie mich wissen, wenn jemand eine Idee hat. Danke, ich schätze jede Rückmeldung.

+0

Diese [SO] (http://stackoverflow.com/questions/863881/how-do-i-tell-if-a-type-is-a-simple-type-ie-holds-a -Einzelwert) Frage? –

+1

Nun überlegen Sie zuerst, ob Sie wirklich Reflexion dafür verwenden möchten; Die Reflexion kann unter Umständen sehr langsam sein. Aus dem Code, den Sie anzeigen, scheint es, als würden Sie versuchen, XML zu serialisieren und zu widerlegen. Dafür gibt es Bibliotheken in .NET - forschen XmlSerializer. Das heißt, Sie müssen eine rekursive Funktion schreiben. –

Antwort

2

Sie können dies nach Ihren Wünschen umgestalten aber es sollte die grundlegende Arbeit erledigen. Es verwendet eine Rekursion, um sich durch alle Eigenschaften in den komplexen Objekten zu bewegen. Es behandelt auch Eigenschaften, die Enumerable sind.

public class PropertyInformation 
{ 
    public string Name { get; set; } 
    public object Value { get; set; } 
} 

public static List<PropertyInformation> ObjectPropertyInformation(object obj) 
{ 
    var propertyInformations = new List<PropertyInformation>(); 

    foreach (var property in obj.GetType().GetProperties()) 
    { 
     //for value types 
     if (property.PropertyType.IsPrimitive || property.PropertyType.IsValueType || property.PropertyType == typeof(string)) 
     { 
      propertyInformations.Add(new PropertyInformation { Name = property.Name, Value = property.GetValue(obj) }); 
     } 
     //for complex types 
     else if (property.PropertyType.IsClass && !typeof(IEnumerable).IsAssignableFrom(property.PropertyType)) 
     { 
      propertyInformations.AddRange(ObjectPropertyInformation(property.GetValue(obj))); 
     } 
     //for Enumerables 
     else 
     { 
      var enumerablePropObj1 = property.GetValue(obj) as IEnumerable; 

      if (enumerablePropObj1 == null) continue; 

      var objList = enumerablePropObj1.GetEnumerator(); 

      while (objList.MoveNext()) 
      { 
       objList.MoveNext(); 
       ObjectPropertyInformation(objList.Current); 
      } 
     } 
    } 

    return propertyInformations; 
} 
+1

Tolle Lösung, Vielen Dank für die schnelle Antwort. Funktioniert super! – CodingRiot

+0

Ein Downvote nach 1 1/2 Jahren? Interessieren Sie sich, warum? –

Verwandte Themen