如何使用反射为通用类型获取Count属性

Bru*_*man 9 c# reflection

我有一个对象列表,其中我无法知道编译时的类型.

我需要识别存在'Count'属性的任何这些对象,如果存在则获取值.

此代码适用于简单的Collection类型:

PropertyInfo countProperty = objectValue.GetType().GetProperty("Count");
 if (countProperty != null)
 {
     int count = (int)countProperty.GetValue(objectValue, null);
 }
Run Code Online (Sandbox Code Playgroud)

问题是这不适用于泛型类型,例如IDictionary<TKey,TValue>.在这些情况下,即使实例化对象中存在"Count"属性,'countProperty'值也会返回null.

我想要做的就是识别任何基于集合/字典的对象并找到它的大小(如果有的话).

编辑:根据要求,这是完整的代码列表,不起作用

private static void GetCacheCollectionValues(ref CacheItemInfo item, object cacheItemValue)
{
    try
        {
        //look for a count property using reflection
        PropertyInfo countProperty = cacheItemValue.GetType().GetProperty("Count");
        if (countProperty != null)
        {
            int count = (int)countProperty.GetValue(cacheItemValue, null);
            item.Count = count;
        }
        else
        {
            //poke around for a 'values' property
            PropertyInfo valuesProperty = cacheItemValue.GetType().GetProperty("Values");
            int valuesCount = -1;
            if (valuesProperty != null)
            {
                object values = valuesProperty.GetValue(cacheItemValue, null);
                if (values != null)
                {
                    PropertyInfo valuesCountProperty = values.GetType().GetProperty("Count");
                    if (countProperty != null)
                    {
                        valuesCount = (int)valuesCountProperty.GetValue(cacheItemValue, null);
                    }
                }
            }
            if (valuesCount > -1)
                item.Count = valuesCount;
            else
                item.Count = -1;
        }
    }
    catch (Exception ex)
    {
        item.Count = -1;
        item.Message = "Exception on 'Count':" + ex.Message;
    }
}
Run Code Online (Sandbox Code Playgroud)

这适用于简单集合,但不适用于从我从中派生的类创建的对象Dictionary<TKey,TValue>.即

CustomClass : 
    Dictionary<TKey,TValue>
Run Code Online (Sandbox Code Playgroud)

CacheItemInfo只是一个包含缓存项属性的简单类 - 即密钥,计数,类型,到期日期时间

Mar*_*ell 13

你应该尝试的第一件事是铸造ICollection,因为它有一个非常便宜.Count:

ICollection col = objectValue as ICollection;
if(col != null) return col.Count;
Run Code Online (Sandbox Code Playgroud)

Count对字典应该工作,虽然-我已经测试这Dictionary<,>和它工作正常-但需要注意的是,即使一些实现IDictionary<,>具体类型(通过返回GetType()) 必须有.Count对公共API -它可以使用显式接口实施满足该接口而不是具有public int Count {get;}.就像我说:它适用于Dictionary<,>- 但不一定适用于所有类型.

如果其他一切都失败了,作为最后的努力:

IEnumerable enumerable = objectValue as IEnumerable;
if(enumerable != null)
{
    int count = 0;
    foreach(object val in enumerable) count++;
    return count;
}
Run Code Online (Sandbox Code Playgroud)

编辑以查看Dictionary<,>评论中提出的问题:

using System;
using System.Collections;
using System.Collections.Generic;
public class CustomClass : Dictionary<int, int> { }
public class CacheItemInfo
{
    public int Count { get; set; }
    public string Message { get; set; }
}
class Program {
    public static void Main() {
        var cii = new CacheItemInfo();
        var data = new CustomClass { { 1, 1 }, { 2, 2 }, { 3, 3 } };
        GetCacheCollectionValues(ref cii, data);
        Console.WriteLine(cii.Count); // expect 3
    }
    private static void GetCacheCollectionValues(ref CacheItemInfo item, object cacheItemValue)
    {
        try
        {
            ICollection col;
            IEnumerable enumerable;
            if (cacheItemValue == null)
            {
                item.Count = -1;
            }
            else if ((col = cacheItemValue as ICollection) != null)
            {
                item.Count = col.Count;
            }
            else if ((enumerable = cacheItemValue as IEnumerable) != null)
            {
                int count = 0;
                foreach (object val in enumerable) count++;
                item.Count = count;
            }
            else
            {
                item.Count = -1;
            }
        }
        catch (Exception ex)
        {
            item.Count = -1;
            item.Message = "Exception on 'Count':" + ex.Message;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)