2012-05-22 107 views
16

我想從列表中的對象中獲取值,該列表是主對象的一部分。使用反射從類的列表中獲取屬性值

我有主對象,其中包含各種屬性,可以是集合。

現在我想弄清楚如何訪問包含在對象中的通用列表。

///<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; 
} 

我試圖用一個SetValue()與MyObject的這一點,但它的錯誤了與(轉述):

對象的類型不

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

您嘗試反映的類的相關部分的代碼可能有點用處。 –

+0

自列表繼承IList你可以通過改變爲'IList oTheList'解決這個問題嗎? –

+1

這是甚至編譯? 'oTheList'是一個'object',它沒有名爲'PropertyType'的屬性。 – FishBasketGordo

回答

18

要獲取/設置使用反射,你需要一個實例。要循環訪問列表中的項目,請嘗試以下操作:

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

它似乎這種方法可能適用於訪問列表。我已經實現了一個函數來檢查類型是否爲Collection。 – Romoku

+0

這應該很簡單 - 'bool isCollection =(oTheList is ICollection);' –

+0

很好,但是沒有解釋如何實現設置嵌套列表。例如MyObject.MyList [0] .SecondList [1] .SomeValue –

4

見如果像這樣的東西可以幫助你朝正確的方向發展:前一段時間我得到了同樣的錯誤,這段代碼縮減解決了我的問題。

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 
    } 
    } 
} 
相關問題