首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用反射和getCount (.net 4)从对象获取集合

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

Stack Overflow用户
提问于 2015-10-29 06:17:51
回答 1查看 957关注 0票数 2

我需要对一个对象进行反思,获取所有的属性,这些属性都是集合,并且

1)每个集合的GetCount

( 2)GetTotalCount (allCollectionCount)

3)使用此集合调用方法。

下面是我到目前为止所做的工作,用一个构成的圆头结构来表示半性。我被困在如何调用这个方法和如何获得计数的收集。

有什么建议吗?

代码语言:javascript
复制
        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; }
        }
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-29 12:30:14

快又脏,给你..。

代码语言:javascript
复制
// ..
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());
    }
    // ..
}
// ..
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33407172

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档