C#C
C#2y ago
Bailey

Reflection and customclass in list properties c#

Hello,

I am trying to solve the folowing. I got a list<person> I want to use reflection to get the list and walk through the properties.
I have put some fictional code in this. The problem is with the list. How to solve this so that I can use the for or foreach loop to go to the next level the custom class properties.

namespace reflection { internal class Program { // just sample static void Main(string[] args) { List<Person> list = new List<Person>(); list.Add(new Person() { Age= 2, Name="a"}); list.Add(new Person() { Age = 3, Name = "b" }); var analyse = new analyse(); analyse.analyseThis(list); Console.ReadLine(); } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace reflection { public class Person { public string Name { get; set; } public int Age { get; set; } } } using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace reflection { public class analyse { public void analyseThis(object test) { var data = test.GetType().GenericTypeArguments[0]; // how to get this working foreach (var customClass in test.GetType().) { // below works for clASS. Type type = customClass.GetType(); PropertyInfo[] propertyInfos = customClass.GetType().GetProperties(); // here the code } } } }
Was this page helpful?