微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 如何使用Reflection在IEnumerable上调用System.Linq.Enumerable.Count <>?

我有一堆IEnumerable集合,确切的数量和类型经常更改(由于自动代码生成).

它看起来像这样:

public class MyCollections {
    public System.Collections.Generic.IEnumerable<SomeType> SomeTypeCollection;
    public System.Collections.Generic.IEnumerable<OtherType> OtherTypeCollection;
    ...

在运行时,我想确定每个Type和它的计数,而不必在每次代码生成后重写代码.所以我正在寻找使用反射的通用方法.我正在寻找的结果是这样的:

MyType: 23
OtherType: 42

我的问题是我无法想象如何正确调用Count方法.这是我到目前为止:

// Handle to the Count method of System.Linq.Enumerable
        MethodInfo countMethodInfo = typeof(System.Linq.Enumerable).getmethod("Count",new Type[] { typeof(IEnumerable<>) });

        PropertyInfo[] properties = typeof(MyCollections).GetProperties();
        foreach (PropertyInfo property in properties)
        {
            Type propertyType = property.PropertyType;
            if (propertyType.IsGenericType)
            {
                Type genericType = propertyType.GetGenericTypeDeFinition();
                if (genericType == typeof(IEnumerable<>))
                {
                    // access the collection property
                    object collection = property.GetValue(someInstanceOfMyCollections,null);

                    // access the type of the generic collection
                    Type genericArgument = propertyType.GetGenericArguments()[0];

                    // make a generic method call for System.Linq.Enumerable.Count<> for the type of this collection
                    MethodInfo localCountMethodInfo = countMethodInfo.MakeGenericmethod(genericArgument);

                    // invoke Count method (this fails)
                    object count = localCountMethodInfo.Invoke(collection,null);

                    System.Diagnostics.Debug.WriteLine("{0}: {1}",genericArgument.Name,count);
                }
            }
        }

解决方法

如果你坚持不懈努力; p

变化:

>如何获取泛型方法的countMethodInfo
> Invoke的参数

代码(注意obj是我的MyCollections实例):

MethodInfo countMethodInfo = typeof (System.Linq.Enumerable).getmethods().Single(
        method => method.Name == "Count" && method.Isstatic && method.GetParameters().Length == 1);

    PropertyInfo[] properties = typeof(MyCollections).GetProperties();
    foreach (PropertyInfo property in properties)
    {
        Type propertyType = property.PropertyType;
        if (propertyType.IsGenericType)
        {
            Type genericType = propertyType.GetGenericTypeDeFinition();
            if (genericType == typeof(IEnumerable<>))
            {
                // access the collection property
                object collection = property.GetValue(obj,null);

                // access the type of the generic collection
                Type genericArgument = propertyType.GetGenericArguments()[0];

                // make a generic method call for System.Linq.Enumerable.Count<> for the type of this collection
                MethodInfo localCountMethodInfo = countMethodInfo.MakeGenericmethod(genericArgument);

                // invoke Count method (this fails)
                object count = localCountMethodInfo.Invoke(null,new object[] {collection});

                System.Diagnostics.Debug.WriteLine("{0}: {1}",count);
            }
        }
    }

原文地址:https://www.jb51.cc/csharp/92170.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐