C#,从对象获取所有集合属性

C#,从对象获取所有集合属性,c#,reflection,collections,C#,Reflection,Collections,我有一个包含3个列表集合的类,如下所示 我试图建立一个逻辑,它将遍历对象的“集合” 属性,并使用存储在这些集合中的数据执行某些操作 我只是想知道是否有一个简单的方法来使用foreach。 谢谢 公共类SampleChartData { 公共列表系列1{get;set;} 公共列表序列2{get;set;} 公共列表系列3{get;set;} 公共样本图表数据() { Series1=新列表(); Series2=新列表(); Series3=新列表(); } } 使用反射获取对象属性。然后迭代这

我有一个包含3个列表集合的类,如下所示

我试图建立一个逻辑,它将遍历对象的“集合” 属性,并使用存储在这些集合中的数据执行某些操作

我只是想知道是否有一个简单的方法来使用foreach。 谢谢

公共类SampleChartData
{
公共列表系列1{get;set;}
公共列表序列2{get;set;}
公共列表系列3{get;set;}
公共样本图表数据()
{
Series1=新列表();
Series2=新列表();
Series3=新列表();
}
}

使用反射获取对象属性。然后迭代这些,查看
是IEnumerable
。然后迭代IEnumerable属性

函数,从对象获取所有IEnumerable:

public static IEnumerable<IEnumerable<T>> GetCollections<T>(object obj)
{
    if(obj == null) throw new ArgumentNullException("obj");
    var type = obj.GetType();
    var res = new List<IEnumerable<T>>();
    foreach(var prop in type.GetProperties())
    {
        // is IEnumerable<T>?
        if(typeof(IEnumerable<T>).IsAssignableFrom(prop.PropertyType))
        {
            var get = prop.GetGetMethod();
            if(!get.IsStatic && get.GetParameters().Length == 0) // skip indexed & static
            {
                var collection = (IEnumerable<T>)get.Invoke(obj, null);
                if(collection != null) res.Add(collection);
            }
        }
    }
    return res;
}
公共静态IEnumerable GetCollections(对象obj)
{
如果(obj==null)抛出新的ArgumentNullException(“obj”);
var type=obj.GetType();
var res=新列表();
foreach(type.GetProperties()中的var prop)
{
//是可数的吗?
if(typeof(IEnumerable).IsAssignableFrom(prop.PropertyType))
{
var get=prop.getMethod();
如果(!get.IsStatic&&get.GetParameters().Length==0)//跳过索引和静态
{
var collection=(IEnumerable)get.Invoke(obj,null);
if(collection!=null)res.Add(collection);
}
}
}
返回res;
}
然后你可以用

var data = new SampleChartData();
foreach(var collection in GetCollections<Point>(data))
{
    foreach(var point in collection)
    {
        // do work
    }
}
var data=新的SampleChartData();
foreach(GetCollections中的var集合(数据))
{
foreach(集合中的变量点)
{
//工作
}
}

迭代所有元素。

您可以使用反射从对象获取属性列表。此示例获取所有属性,并将其名称和计数打印到控制台:

public static void PrintSeriesList()
{
    SampleChartData myList = new SampleChartData();

    PropertyInfo[] Fields = myList.GetType().GetProperties();

    foreach(PropertyInfo field in Fields)
    {
        var currentField =  field.GetValue(myList, null);
        if (currentField.GetType() == typeof(List<Point>))
        {
            Console.WriteLine("List {0} count {1}", field.Name, ((List<Point>)currentField).Count);
        }
    }
}
publicstaticvoidprintserieslist()
{
SampleChartData myList=新建SampleChartData();
PropertyInfo[]Fields=myList.GetType().GetProperties();
foreach(字段中的PropertyInfo字段)
{
var currentField=field.GetValue(myList,null);
if(currentField.GetType()==typeof(列表))
{
WriteLine(“List{0}count{1}”,field.Name,((List)currentField.count);
}
}
}

刚刚找到了一个快速解决方案,但也许你们中的一些人有更好的方法。 这就是我所做的

SampleChartData myData = DataFeed.GetData();
Type sourceType = typeof(SampleChartData);
foreach (PropertyInfo pi in (sourceType.GetProperties()))
{
    if (pi.GetValue(myData, null).GetType() == typeof(List<Point>))
    {
        List<Point> currentSeriesData = (List<Point>)pi.GetValue(myData, null);

        // then do something with the data
    }
}
samplechartdatamydata=DataFeed.GetData();
类型sourceType=typeof(SampleChartData);
foreach(sourceType.GetProperties()中的PropertyInfo pi)
{
if(pi.GetValue(myData,null).GetType()==typeof(List))
{
List currentSeriesData=(List)pi.GetValue(myData,null);
//然后对数据进行处理
}
}

需要注意的是,字符串也显示为IEnumerableAre您是在寻找一种通用机制来查找任何对象中的所有集合,还是专门寻找一种方法来公开图表中的所有系列?在后一种情况下,我可以看到类似于
IChartSeriesContainer
的东西,它有一个
GetAllSeries
方法,返回
IEnumerable
。嗨,我在寻找一种通用机制来查找对象中特定类型的集合。但最后我决定用一张“单子”
SampleChartData myData = DataFeed.GetData();
Type sourceType = typeof(SampleChartData);
foreach (PropertyInfo pi in (sourceType.GetProperties()))
{
    if (pi.GetValue(myData, null).GetType() == typeof(List<Point>))
    {
        List<Point> currentSeriesData = (List<Point>)pi.GetValue(myData, null);

        // then do something with the data
    }
}