Search code examples
c#propertiesstack-overflowienumerableienumerator

Iterating through Object Properties in C#


I have a list of Customer Type Objects and as I iterate through that list, I would like to be able to iterate through each Customer's properties. I would then like to print out that property value as a string. I am getting a StackOverFlowException error.

Let me preface this by saying:

  1. This is only a class library, I am calling the function from elsewhere and the call works.
  2. The call to the database and the information it returns is correct (I tested it before trying to iterate through the properties)
  3. My end goal is to convert the List of Customers to a 2D Array where each column represents a customer and each row represents the Customer properties.

Thanks in advance!

    using Dapper;
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Xml.Linq;
    using System.Reflection.Emit;
    using System.Reflection;
    using System.Collections;
    
    namespace AtoCLib
    {
        public class DataAccessLayer
        {
            public static List<Customer> GetCustomerList(string startChar)
            {
                string sql = $"SELECT TOP (10) P.LastName, [CustomerID],[PersonID] ,[StoreID] ,[TerritoryID] ,[AccountNumber] FROM [AdventureWorks2017].[Sales].[Customer] C INNER JOIN [Person].[Person] P ON C.CustomerID = P.BusinessEntityID WHERE P.LastName >= '{startChar}'";
                List<Customer> CustomerList = new List<Customer>();
          
                try
                {
                    using (var connection = new SqlConnection("Data Source=SHCP-2035;Initial Catalog=AdventureWorks2017;Integrated Security=True"))
                    {
                        var Customers = connection.Query<Customer>(sql).AsList();
    
                        foreach (Customer customer in Customers)
                        {
                            CustomerList.Add(customer);
                        }
                    }
                }
                catch (Exception e)
                {
    
                    Console.Write(e);
                }
    
                return CustomerList;
            }
    
            public static void getCustListArray(string nameChar)
            {
                List<Customer> list = GetCustomerList(nameChar);
                string[,] customerArray = new string[10, 6];
    
                foreach (Customer customerObj in list)
                {
                    Customer tempCustomer = new Customer();
                    tempCustomer = customerObj;
    
                    foreach (PropertyInfo property in tempCustomer)
                    {
                        Console.WriteLine(property.GetValue(tempCustomer));
                    }
                }
            }
        }
    
        public class Customer : IEnumerable<PropertyInfo>
        {
            public int CustomerID { get; set; }
            public int? PersonID { get; set; }
            public int? StoreID { get; set; }
            public int? TerritoryID { get; set; }
            public string AccountNumber { get; set; }
            public string lastName { get; set; }
    
            public IEnumerator<PropertyInfo> GetEnumerator()
            {
                return GetEnumerator();
            }
    
            IEnumerator IEnumerable.GetEnumerator()
            {
                return GetEnumerator();
            }
        }
    }

The error is:

Process is terminated due to StackOverflowException.


Solution

  • public IEnumerator<PropertyInfo> GetEnumerator()
    {
        return GetEnumerator();
    }
    

    This method is calling itself, eventually your stack will overflow. Implement it properly:

    public IEnumerator<PropertyInfo> GetEnumerator()
    {
        foreach (var property in typeof(Customer).GetProperties())
        {
            yield return property;
        }
    }