2012-05-22 25 views
16

Ich versuche, die Werte von Objekten innerhalb einer Liste zu erhalten, die Teil eines Hauptobjekts ist.Verwenden von Reflektion zum Abrufen von Werten aus Eigenschaften aus einer Liste einer Klasse

Ich habe das Hauptobjekt, das verschiedene Eigenschaften enthält, die Sammlungen sein können.

Im Moment versuche ich herauszufinden, wie man auf eine generische Liste, die im Objekt enthalten ist, zugreifen kann.

///<summary> 
///Code for the inner class 
///</summary> 
public class TheClass 
{ 
    public TheClass(); 

    string TheValue { get; set; } 
} //Note this class is used for serialization so it won't compile as-is 

///<summary> 
///Code for the main class 
///</summary> 
public class MainClass 
{ 
    public MainClass(); 

    public List<TheClass> TheList { get; set; } 
    public string SomeOtherProperty { get; set; } 
    public Class SomeOtherClass { get; set } 
} 


public List<MainClass> CompareTheValue(List<object> MyObjects, string ValueToCompare) 
{ 
    //I have the object deserialised as a list 
    var ObjectsToReturn = new List<MainClass>(); 
    foreach(var mObject in MyObjects) 
    { 

     //Gets the properties 
     PropertyInfo piTheList = mObject.GetType().GetProperty("TheList"); 

     object oTheList = piTheList.GetValue(MyObject, null); 


     //Now that I have the list object I extract the inner class 
     //and get the value of the property I want 
     PropertyInfo piTheValue = oTheList.PropertyType 
              .GetGenericArguments()[0] 
              .GetProperty("TheValue"); 

     //get the TheValue out of the TheList and compare it for equality with 
     //ValueToCompare 
     //if it matches then add to a list to be returned 

     //Eventually I will write a Linq query to go through the list to do the comparison. 
     ObjectsToReturn.Add(objectsToReturn); 

    } 
return ObjectsToReturn; 
} 

Ich habe ein SetValue() mit MyObject dazu verwenden versucht, aber es Fehler aus mit (paraphrasiert):

Objekt ist nicht vom Typ

private bool isCollection(PropertyInfo p) 
{ 
    try 
    { 
     var t = p.PropertyType.GetGenericTypeDefinition(); 
     return typeof(Collection<>).IsAssignableFrom(t) || 
       typeof(Collection).IsAssignableFrom(t); 
    } 
    catch 
    { 
     return false; 
    } 
    } 
} 
+2

Der Code für die relevanten Teile der Klasse, die Sie versuchen zu reflektieren, könnte ein bisschen nützlich sein. –

+0

seit List IList erbt, können Sie dies umgehen, indem Sie in 'IList oTheList' wechseln? –

+1

Kompiliert das überhaupt? 'oTheList' ist ein' Objekt', das keine Eigenschaft namens 'PropertyType' hat. – FishBasketGordo

Antwort

18

Zum Abrufen/Einstellen über Reflektion benötigen Sie eine Instanz. Um die Einträge in der Liste durchzulaufen, versuchen Sie Folgendes:

PropertyInfo piTheList = MyObject.GetType().GetProperty("TheList"); //Gets the properties 

IList oTheList = piTheList.GetValue(MyObject, null) as IList; 

//Now that I have the list object I extract the inner class and get the value of the property I want 

PropertyInfo piTheValue = piTheList.PropertyType.GetGenericArguments()[0].GetProperty("TheValue"); 

foreach (var listItem in oTheList) 
{ 
    object theValue = piTheValue.GetValue(listItem, null); 
    piTheValue.SetValue(listItem,"new",null); // <-- set to an appropriate value 
} 
+0

es scheint, dass diese Methode für den Zugriff auf die Liste funktioniert. Ich habe eine Funktion implementiert, um zu überprüfen, ob der Typ eine Sammlung ist. – Romoku

+0

Das sollte einfach sein - 'bool isCollection = (oTheList ist ICollection);' –

+0

Schön, aber es fehlt eine Erklärung, wie man verschachtelte Listen einstellen kann. Zum Beispiel MyObject.MyList [0] .SecondList [1] .SomeValue –

4

See Wenn dir so etwas in die richtige Richtung hilft: Ich habe vor einer Weile den gleichen Fehler bekommen und dieser Code hat mein Problem gelöst.

PropertyInfo[] properties = MyClass.GetType().GetProperties(); 
foreach (PropertyInfo property in properties) 
{ 
    if (property.Name == "MyProperty") 
    { 
    object value = results.GetType().GetProperty(property.Name).GetValue(MyClass, null); 
    if (value != null) 
    { 
    //assign the value 
    } 
    } 
} 
Verwandte Themen