使用反射和getCount(.net 4)从对象获取集合

开发人员9969

我有一个要求,以反映一个对象上获取所有属性是集合和

1)每个集合的GetCount

2)GetTotalCount(allCollectionCount)

3)用这个集合调用一个方法。

以下是到目前为止我为虚假性编造的点头结构所做的工作。我被困在如何调用此方法以及如何获取计数。

有什么建议?

        using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;
    using System.Text;

    namespace ConsoleApplication2
    {
        class Program
        {
            static void Main()
            {
                var request = GetDataRequest();

                //Get all properties 
                List<PropertyInfo> propInfoList =
                    new List<PropertyInfo>(request.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public));

                //Get collections only
                var myClassCollections=propInfoList.Where(xxx => xxx.PropertyType.GetInterfaces().Any(x => x == typeof (IEnumerable))).ToList();

                var totalCountForAllCollections=????
                foreach (var col in myClassCollections)
                {
                    //How do I call my Method DoSomething
                    //      DoSomething<?>(col.?????)   
                }

            }

            public void DoSomething<T>(List<T> objectCollection)
            {
                //etc...
            }
            private static DataRequest GetDataRequest()
            {
                DataRequest request = new DataRequest();
                request.Addresses.Add(new Address
                {
                    Id = 1,
                    City = "London",
                    Postcode = "32131",
                    Street = "London Road"
                });
                request.Addresses.Add(new Address
                {
                    Id = 2,
                    City = "NewYork",
                    Postcode = "3432",
                    Street = "NewYork Road"
                });

                request.Customers.Add(new Customer
                {
                    Id = 1,
                    Name = "Jo",
                    Surname = "Bloggs",
                });
                request.Customers.Add(new Customer
                {
                    Id = 1,
                    Name = "Jon",
                    Surname = "Bloggs2",
                });
                request.Customers.Add(new Customer
                {
                    Id = 1,
                    Name = "Jonny",
                    Surname = "Bloggs3",
                });
                return request;
            }
        }
        public class DataRequest
        {
            public DataRequest()
            {
                Customers = new List<Customer>();
                Orders = new List<Order>();
                Addresses = new List<Address>();

            }
            public List<Customer> Customers { get; set; }
            public List<Order> Orders { get; set; }
            public List<Address> Addresses { get; set; }

        }

        public class Customer
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public string Surname { get; set; }
        }

        public class Order
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public string OrderNo { get; set; }
        }

        public class Address
        {
            public int Id { get; set; }
            public string Street { get; set; }
            public string City { get; set; }
            public string Postcode { get; set; }
        }
    }
阿夫扎尔·哈森(AfzalHassen)

又快又脏,你去...

// ..
static class Program
{
    static void Main()
    {
        var request = GetDataRequest();

        //Get propertyValues for properties that are enumerable (i.e. lists,arrays etc)
        var collectionProperties = request.GetType()
                                          .GetProperties(BindingFlags.Instance | BindingFlags.Public)
                                          .Where(propertInfo => propertInfo.PropertyType.GetInterfaces().Any(x => x == typeof(IEnumerable)))
                                          .Select(p => p.GetValue(request, null))
                                          .Cast<IEnumerable<object>>().ToList();

        var totalCountForAllCollections = 0;
        // iterate through the list of propertyValues
        foreach (var collectionPropertyValue in collectionProperties)
        {
            totalCountForAllCollections += collectionPropertyValue.Count();
            collectionPropertyValue.DoSomething();
        }

        System.Console.WriteLine("The total count for all collections is : {0}", totalCountForAllCollections);
        System.Console.WriteLine("press any key to exit");
        System.Console.ReadLine();
    }

    public static void DoSomething<T>(this IEnumerable<T> objectCollection)
    {
        //etc...
        // N.B. you will have to use typeof(T) to implement logic specific to the type
        // If the logic in this method is non-specific to the typeof(T) then Implement logic accordingly
        System.Console.WriteLine("The type of the collection is: {0}", objectCollection.GetType());
        System.Console.WriteLine("The count of items in this collection is:{0}", objectCollection.Count());
    }
    // ..
}
// ..

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用反射从集合类获取对象

来自分类Dev

使用反射将类中的所有静态属性作为对象获取VB.NET

来自分类Dev

如何使.NET反射与动态生成的对象一起使用?

来自分类Dev

如何使.NET反射与动态生成的对象一起使用?

来自分类Dev

.net使用反射在多级上获取属性

来自分类Dev

如何使用Ember ArrayController获取对象集合?

来自分类Dev

使用键 VBA 获取集合内的对象

来自分类Dev

如何使用.net 4 api端点从Request.Content对象获取原始请求正文

来自分类Dev

如何使用反射和递归获取任何对象的所有名称和值

来自分类Dev

如何使用反射和递归获取任何对象的所有名称和值

来自分类Dev

如何在Scala中使用反射获取所有对象值和子对象值?

来自分类Dev

如何在Scala中使用反射获取所有对象值和子对象值?

来自分类Dev

在.NET Core中使用反射

来自分类Dev

使用ASP.NET MVC 4(嵌套集合)创建“博客评论和回复”部分

来自分类Dev

使用Dart中的反射从ClassMirror获取吸气剂和/或属性?

来自分类Dev

使用反射获取动态创建的NPOCO对象键和值C#

来自分类Dev

根据ActiveRecord assoc和值获取对象集合

来自分类Dev

如何使用反射获取Action的Http Verb属性-ASP.NET Core?

来自分类Dev

使用反射,获取对象的父类/字段

来自分类Dev

使用通用类型对象的反射获取属性

来自分类Dev

使用反射从列表中获取对象

来自分类Dev

通过反射.NET获取静态属性

来自分类Dev

AngularJS使用ngModel从Checkbox集合获取复杂对象

来自分类Dev

使用部分加载的集合获取实体框架对象

来自分类Dev

如何使用jQuery获取对象集合的子元素的值?

来自分类Dev

使用linq和反射将属性绑定到ASP.NET中的Web控件

来自分类Dev

.NET中是否有一种无序集合,可以快速添加和删除对象?

来自分类Dev

使用Json.Net C#获取json对象中的值和键

来自分类Dev

使用Json.Net C#获取json对象中的值和键

Related 相关文章

  1. 1

    使用反射从集合类获取对象

  2. 2

    使用反射将类中的所有静态属性作为对象获取VB.NET

  3. 3

    如何使.NET反射与动态生成的对象一起使用?

  4. 4

    如何使.NET反射与动态生成的对象一起使用?

  5. 5

    .net使用反射在多级上获取属性

  6. 6

    如何使用Ember ArrayController获取对象集合?

  7. 7

    使用键 VBA 获取集合内的对象

  8. 8

    如何使用.net 4 api端点从Request.Content对象获取原始请求正文

  9. 9

    如何使用反射和递归获取任何对象的所有名称和值

  10. 10

    如何使用反射和递归获取任何对象的所有名称和值

  11. 11

    如何在Scala中使用反射获取所有对象值和子对象值?

  12. 12

    如何在Scala中使用反射获取所有对象值和子对象值?

  13. 13

    在.NET Core中使用反射

  14. 14

    使用ASP.NET MVC 4(嵌套集合)创建“博客评论和回复”部分

  15. 15

    使用Dart中的反射从ClassMirror获取吸气剂和/或属性?

  16. 16

    使用反射获取动态创建的NPOCO对象键和值C#

  17. 17

    根据ActiveRecord assoc和值获取对象集合

  18. 18

    如何使用反射获取Action的Http Verb属性-ASP.NET Core?

  19. 19

    使用反射,获取对象的父类/字段

  20. 20

    使用通用类型对象的反射获取属性

  21. 21

    使用反射从列表中获取对象

  22. 22

    通过反射.NET获取静态属性

  23. 23

    AngularJS使用ngModel从Checkbox集合获取复杂对象

  24. 24

    使用部分加载的集合获取实体框架对象

  25. 25

    如何使用jQuery获取对象集合的子元素的值?

  26. 26

    使用linq和反射将属性绑定到ASP.NET中的Web控件

  27. 27

    .NET中是否有一种无序集合,可以快速添加和删除对象?

  28. 28

    使用Json.Net C#获取json对象中的值和键

  29. 29

    使用Json.Net C#获取json对象中的值和键

热门标签

归档