如何通过反射过滤任何属性的集合?

发布于 2024-12-16 12:43:02 字数 332 浏览 5 评论 0原文

我有 IEnumerable 集合。我想创建这样的方法:

public IEnumerable<object> Try_Filter(IEnumerable<object> collection, string property_name, string value)
{
    //If object has property with name property_name,
    // return collection.Where(c => c.Property_name == value)
}

可能吗?我正在使用 C# 4.0。 谢谢!

I have IEnumerable collection. I want to create such method:

public IEnumerable<object> Try_Filter(IEnumerable<object> collection, string property_name, string value)
{
    //If object has property with name property_name,
    // return collection.Where(c => c.Property_name == value)
}

Is it possible? I'm using C# 4.0.
Thanks!

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

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

发布评论

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

评论(1

心舞飞扬 2024-12-23 12:43:02

试试这个:

public IEnumerable<object> Try_Filter(IEnumerable<object> collection,
 string property_name, string value)
    {
        var objTypeDictionary = new Dictionary<Type, PropertyInfo>();
        var predicateFunc = new Func<Object, String, String, bool>((obj, propName, propValue) => {
            var objType = obj.GetType();
            PropertyInfo property = null;
            if(!objTypeDictionary.ContainsKey(objType))
            {           
                property = objType.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).FirstOrDefault(prop => prop.Name == propName);
                objTypeDictionary[objType] = property;
            } else {
                property = objTypeDictionary[objType];
            }
            if(property != null && property.GetValue(obj, null).ToString() == propValue)
                return true;

            return false;
        });
        return collection.Where(obj => predicateFunc(obj, property_name, value));
    }

测试:

class a
{
    public string t { get; set;}
}
var lst = new List<Object> { new a() { t = "Hello" }, new a() { t = "HeTherello" }, new a() { t = "Hello" } };
var result = Try_Filter(lst, "t", "Hello");
result.Dump();

虽然,这对于大型集合来说会非常慢

Try this:

public IEnumerable<object> Try_Filter(IEnumerable<object> collection,
 string property_name, string value)
    {
        var objTypeDictionary = new Dictionary<Type, PropertyInfo>();
        var predicateFunc = new Func<Object, String, String, bool>((obj, propName, propValue) => {
            var objType = obj.GetType();
            PropertyInfo property = null;
            if(!objTypeDictionary.ContainsKey(objType))
            {           
                property = objType.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).FirstOrDefault(prop => prop.Name == propName);
                objTypeDictionary[objType] = property;
            } else {
                property = objTypeDictionary[objType];
            }
            if(property != null && property.GetValue(obj, null).ToString() == propValue)
                return true;

            return false;
        });
        return collection.Where(obj => predicateFunc(obj, property_name, value));
    }

Tested with:

class a
{
    public string t { get; set;}
}
var lst = new List<Object> { new a() { t = "Hello" }, new a() { t = "HeTherello" }, new a() { t = "Hello" } };
var result = Try_Filter(lst, "t", "Hello");
result.Dump();

Although, this will be very slow for large collections

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