遍历C#中的对象属性

时间:2018-07-11 16:32:45

标签: c# properties stack-overflow ienumerable ienumerator

我有一个客户类型对象的列表,当我遍历该列表时,我希望能够遍历每个客户的属性。然后,我想将该属性值打印为字符串。我收到一个StackOverFlowException错误。

让我以此开头:

  1. 这只是一个类库,我从其他地方调用该函数,此调用有效。
  2. 对数据库的调用及其返回的信息是正确的(我在尝试遍历属性之前对其进行了测试)
  3. 我的最终目标是将“客户列表”转换为二维数组,其中每一列代表一个客户,每一行代表“客户”属性。

谢谢!

    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();
            }
        }
    }

错误是:

Process is terminated due to StackOverflowException.

2 个答案:

答案 0 :(得分:0)

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

此方法正在自我调用,最终您的堆栈将溢出。正确实施:

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

答案 1 :(得分:0)

是的,您会收到此错误,因为方法Customer.GetEnumerator()会先调用自身,然后又调用它,并且它会创建无限递归。要获取对象的所有公共属性,请在此方法中使用以下代码:

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

但是,我认为这不是在GetEnumerator()方法中执行此操作的正确方法。您的课程不是集合或类似内容。因此,直接从方法GetProperties()中使用方法getCustArray()

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