Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using reflection to get values from properties from a list of a class

Tags:

I am trying to get the values from objects inside a list which is part of a main object.

I have the main object which contains various properties which can be collections.

Right now I am trying to figure out how to access a generic list which is contained in the object.

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

I've tried to use a SetValue() with MyObject on this, but it errors out with (paraphrased):

object is not of type

private bool isCollection(PropertyInfo p) {     try     {         var t = p.PropertyType.GetGenericTypeDefinition();         return typeof(Collection<>).IsAssignableFrom(t) ||                typeof(Collection).IsAssignableFrom(t);     }     catch     {         return false;     }     } } 
like image 801
Dustin Kingen Avatar asked May 22 '12 21:05

Dustin Kingen


People also ask

How does reflection set property value?

To set property values via Reflection, you must use the Type. GetProperty() method, then invoke the PropertyInfo. SetValue() method. The default overload that we used accepts the object in which to set the property value, the value itself, and an object array, which in our example is null.

How to Get property Type by reflection c#?

You can do this by getting an array of all properties from the Type. GetProperties method and then iterating the elements in the array, or you can retrieve the PropertyInfo object that represents the property directly by calling the Type. GetProperty method and specifying the property name.

How to use GetProperty in c#?

GetProperties() Method Syntax: public System. Reflection. PropertyInfo[] GetProperties (); Return Value: This method returns an array of PropertyInfo objects representing all public properties of the current Type or an empty array of type PropertyInfo if the current Type does not have public properties.


1 Answers

To Get/Set using reflection you need an instance. To loop through the items in the list try this:

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 } 
like image 119
D Stanley Avatar answered Oct 26 '22 13:10

D Stanley