2012-05-22 21 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; 
} 

私はこの上のMyObjectにしてSetValue()を使用しようとしたが、それ(言い換え)でエラーアウトは:

オブジェクトがタイプでない

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'は' PropertyType'という名前のプロパティを持たない 'object'です。 – 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

itこのメソッドはリストへのアクセスに役立つようです。私は、型が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 
    } 
    } 
} 
関連する問題