C# 获取具有嵌套列表属性的列表的所有值

C# 获取具有嵌套列表属性的列表的所有值,c#,linq,console-application,propertyinfo,C#,Linq,Console Application,Propertyinfo,今天早上我遇到了一个似乎很容易解决的问题。 我想将列表的所有值写入我的控制台。 在本例中,列表包含列表成员。我已经寻找了一段时间的解决方案,但我找不到 我已经做到了 tl.ForEach(tradelane => { row = ""; foreach(PropertyInfo pi in typeof(coTradeLane).GetProperties()) { T

今天早上我遇到了一个似乎很容易解决的问题。 我想将列表的所有值写入我的控制台。 在本例中,列表包含列表成员。我已经寻找了一段时间的解决方案,但我找不到

我已经做到了

tl.ForEach(tradelane =>
        {
            row = "";

            foreach(PropertyInfo pi in typeof(coTradeLane).GetProperties())
            {
                Type T = pi.PropertyType;

                if (T.IsGenericType && T.GetGenericTypeDefinition() == typeof(List<>))
                {
                    foreach(PropertyInfo piList in tradelane.GetType().GetProperties())
                    {

                            // Select the nested list and loop through each member..

                    }
                    continue;
                }

                var val = pi.GetValue(tradelane);
                if (val != null) row += val.ToString() + " \t ";
                else row += " \t \t ";
            }
            Console.WriteLine(row);
        });
tl.ForEach(tradelane=>
{
行=”;
foreach(PropertyInfo pi in typeof(coTradeLane).GetProperties())
{
类型T=pi.PropertyType;
if(T.IsGenericType&&T.GetGenericTypeDefinition()==typeof(列表))
{
foreach(tradelane.GetType().GetProperties()中的PropertyInfo piList)
{
//选择嵌套列表并循环遍历每个成员。。
}
继续;
}
var val=pi.GetValue(贸易通道);
如果(val!=null)行+=val.ToString()+“\t”;
else行+=“\t\t”;
}
控制台写入线(世界其他地区);
});

我不完全确定您想要什么,但是这个递归解决方案可能会帮助您。 我有点作弊,因为我在寻找
IList
而不是
List
来简化代码

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace Demo
{
    // This type contains two properties.
    // One is a plain List<Double>, the other is a type that itself contains Lists.

    public sealed class Container
    {
        public List<double> Doubles { get; set; }

        public Lists Lists { get; set; }
    }

    // This type contains two Lists.

    public sealed class Lists
    {
        public List<string> Strings { get; set; }
        public List<int> Ints { get; set; }
    }

    public static class Program
    {
        private static void Main()
        {
            var lists = new Lists
            {
                Strings = new List<string> {"A", "B", "C"}, 
                Ints = new List<int> {1, 2, 3, 4, 5}
            };

            var container = new Container
            {
                Doubles = new List<double> {1.1, 2.2, 3.3, 4.4},
                Lists = lists
            };

            var items = FlattenLists(container);

            // This prints:
            //
            // 1.1
            // 2.2
            // 3.3
            // 4.4
            // A
            // B
            // C
            // 1
            // 2
            // 3
            // 4
            // 5

            foreach (var item in items)
                Console.WriteLine(item);
        }

        // This recursively looks for all IList properties in the specified object and its subproperties.
        // It returns each element of any IList that it finds.

        public static IEnumerable<object> FlattenLists(object container)
        {
            foreach (var pi in container.GetType().GetProperties().Where(p => p.GetMethod.GetParameters().Length == 0))
            {
                var prop = pi.GetValue(container);

                if (typeof(IList).IsAssignableFrom(pi.PropertyType))
                {
                    foreach (var item in (IList) prop)
                        yield return item;
                }

                foreach (var item in FlattenLists(prop))
                    yield return item;
            }
        }
    }
}
使用系统;
使用系统集合;
使用System.Collections.Generic;
使用System.Linq;
名称空间演示
{
//此类型包含两个属性。
//一个是普通列表,另一个是本身包含列表的类型。
公共密封类集装箱
{
公共列表加倍{get;set;}
公共列表{get;set;}
}
//此类型包含两个列表。
公开密封的班级名单
{
公共列表字符串{get;set;}
公共列表整数{get;set;}
}
公共静态类程序
{
私有静态void Main()
{
变量列表=新列表
{
Strings=新列表{“A”、“B”、“C”},
Ints=新列表{1,2,3,4,5}
};
var容器=新容器
{
Doubles=新列表{1.1,2.2,3.3,4.4},
列表=列表
};
var项目=列表(容器);
//这张照片是:
//
// 1.1
// 2.2
// 3.3
// 4.4
//A
//B
//C
// 1
// 2
// 3
// 4
// 5
foreach(项目中的var项目)
控制台写入线(项目);
}
//这将递归查找指定对象及其子属性中的所有IList属性。
//它返回找到的任何IList的每个元素。
公共静态IEnumerable列表(对象容器)
{
foreach(容器中的var pi.GetType().GetProperties().Where(p=>p.GetMethod.GetParameters().Length==0))
{
var prop=pi.GetValue(容器);
if(typeof(IList).IsAssignableFrom(pi.PropertyType))
{
foreach(IList道具中的var项目)
收益回报项目;
}
foreach(列表中的var项目(prop))
收益回报项目;
}
}
}
}

但我不确定这有多大用处,因为你只是得到了一个扁平的
对象列表
,而不知道它们所关联的属性。但是,您可以修改
flattlists()
以返回更多信息,而不仅仅是对象。

请查看感谢Matthew!我马上就去试试。:)