© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
17 replies
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
            }
        }

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

    }
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Properties vs Fields in C#
C#CC# / help
13mo ago
✅ Get bytes and append to List c#
C#CC# / help
16mo ago
✅ How to get values of list using reflection?
C#CC# / help
3y ago
❔ ✅ Reflection to get all Properties of a certain type
C#CC# / help
3y ago