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

后端 未结 2 1059
感动是毒
感动是毒 2020-12-07 00:27

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 collec

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-07 01:02

    See if something like this helps you in the right direction: I got the same error a while back and this code snipped solved my issue.

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

提交回复
热议问题