2

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.

0

2 Answers 2

10
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;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Still relevant in 2021. Ty! foreach (var property in typeof(Models.EF.Tables.VBatchDirectoryOutput).GetProperties()) { headerString += property.Name.ToString() + ','; }
5

Yes, you're getting this error because your method Customer.GetEnumerator() calls itself and then again, and it's create an inifinite recursion. For getting all public properties of an object use the following code in this method:

return this.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);

But, I think this isn't the correct way to do this inside GetEnumerator() method. Your class is not a collection, or something like. So, use the method GetProperties() directly from the method getCustArray():

foreach (PropertyInfo property in tempCustomer.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
    Console.WriteLine(property.GetValue(tempCustomer));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.