迭代通用的对象列表并打印值

时间:2018-06-08 05:56:11

标签: c# list

我想要一个实用程序函数,它读取一般对象列表中的值并打印它们,如下例所示(请参阅代码中的注释)。我可以在输出中获取RowIndex,BookingId等属性名称,但不知道如何获取分配给这些属性的值。在运行时我不知道对象属性(名称)所以例如,让我们说。有人可以帮助我。

public class ReadList
{
    public void ReadList<T>(List<T> list)
    {
        try
        {                              
            for (int i = 0; i < list.Count; i++)
            {
                Type myObject = list[i].GetType();
                List<string> propertyInfo = myObject.GetProperties().Select(p => p.Name).ToList();                    
                foreach (var propName in propertyInfo)
                {
                    Console.WriteLine("Property Name : " + propName);
                }
                //How to get  access to the property values?
                //For example How can I access the Property names and values and print them on the screen like below
                //Property Name : RowIndex , Property Value : 1      
                //Property Name : RowIndex , Property Value : 2
                //Property Name : RowIndex , Property Value : 3
                //Property Name : RowIndex , Property Value : 4                  
            }               
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
            //throw;
        }
    }
    public void Read()
    {
        List<RecurringBookingDetails> list = new List<RecurringBookingDetails>();
        list.Add(new RecurringBookingDetails() { RowIndex = 1 });
        list.Add(new RecurringBookingDetails() { RowIndex = 2 });
        list.Add(new RecurringBookingDetails() { RowIndex = 3 });
        list.Add(new RecurringBookingDetails() { RowIndex = 4 });
        ReadList(list);

        List<CasualBookingDetails> list2 = new List<CasualBookingDetails>();
        list2.Add(new CasualBookingDetails() { BookingID = 1 ,BookingName="A"});
        list2.Add(new CasualBookingDetails() { BookingID = 2 ,BookingName="B"});
        list2.Add(new CasualBookingDetails() { BookingID = 3 ,BookingName="C"});
        list2.Add(new CasualBookingDetails() { BookingID = 4 ,BookingName="D"});
        ReadList(list2);
    }
}

public class RecurringBookingDetails
{       
    public int RowIndex { get; set; }
}

public class CasualBookingDetails
{       
    public int BookingID { get; set; }
    public int BookingName{ get; set; }
}

1 个答案:

答案 0 :(得分:1)

在调用PropertyInfo后,您似乎立即将所有GetProperties映射到了他们的名字。现在,您不再拥有PropertyInfo个对象,而这些对象是您需要的,以获取属性&#39;值。您需要致电PropertyInfo.GetValue

public void ReadList<T>(List<T> list) {
    try {
        for (int i = 0; i < list.Count; i++) {
            Type myObject = list[i].GetType();
            // just get the array of properties without "Select"
            var propertyInfo = myObject.GetProperties();
            foreach (var prop in propertyInfo) {
                Console.WriteLine("Property Name : " + prop.Name);
                // Here you can call "GetValue", with the object being "list[i]"
                Console.WriteLine("Property Value : " + prop.GetValue(list[i]));
            }
        }
    } catch (Exception e) {
        Console.WriteLine(e.Message);
        //throw;
    }
}