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

发布于 2024-09-04 22:03:44 字数 555 浏览 3 评论 0原文

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

我试图有一个逻辑来迭代对象的“集合” 属性并使用这些集合中存储的数据执行一些操作。

我只是想知道是否有一种简单的方法可以使用 foreach 来做到这一点。 谢谢

public class SampleChartData
    {
        public List<Point> Series1 { get; set; }
        public List<Point> Series2 { get; set; }
        public List<Point> Series3 { get; set; }

        public SampleChartData()
        {
            Series1 = new List<Point>();
            Series2 = new List<Point>();
            Series3 = new List<Point>();
        }
    }

I have a class with 3 List collections like the following.

I am trying to have a logic which will iterate through the object's "collection"
properties and do some operation using the data stored in those collections.

I am just wondering if there is an easy way of doing it using foreach.
thanks

public class SampleChartData
    {
        public List<Point> Series1 { get; set; }
        public List<Point> Series2 { get; set; }
        public List<Point> Series3 { get; set; }

        public SampleChartData()
        {
            Series1 = new List<Point>();
            Series2 = new List<Point>();
            Series3 = new List<Point>();
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

秋千易 2024-09-11 22:03:44

获取所有 IEnumerable的函数from object:

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

然后您可以使用类似的方法

var data = new SampleChartData();
foreach(var collection in GetCollections<Point>(data))
{
    foreach(var point in collection)
    {
        // do work
    }
}

来迭代所有元素。

Function to get all IEnumerable<T> from object:

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

Then you can use something like

var data = new SampleChartData();
foreach(var collection in GetCollections<Point>(data))
{
    foreach(var point in collection)
    {
        // do work
    }
}

to iterate through all elements.

素食主义者 2024-09-11 22:03:44

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

Use Reflection to get the objects Properties. Then iterate over those to see is IEnumerable<T>. Then iterate over the IEnumerable properties

深海里的那抹蓝 2024-09-11 22:03:44

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

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

You can use reflection to get the list of properties from the object. This example gets all the properties and prints their name and Count to the console:

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);
        }
    }
}
溺渁∝ 2024-09-11 22:03:44

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

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
    }
}

Just found a quick solution, but maybe some of you have better ways of doing it.
this is what I did.

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
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文