使用具有不同类型/类别列表的通用方法 [英] Use a generic method with a list of different type/class

查看:92
本文介绍了使用具有不同类型/类别列表的通用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为List创建搜索功能。这很简单,但我想要一个方法,它可以使用不同类型的列表。以下面的例子来清除问题:

方法SearchGridViewCategorie和SearchGridViewMedewerker都是搜索List的方法,如果它包含搜索项。

  public static void SearchGridViewMedewerker(ref List< Medewerker> medewerkers,String term){
medewerkers = medewerkers.Where(m => m.gebruikersnaam .ToLower()。Contains(term.ToLower())||
m.naam.ToLower()。Contains(term.ToLower())||
m.email.ToLower()。Contains (term.ToLower())||
m.rol.ToLower()。Contains(term.ToLower()))。ToList();
}

public static void SearchGridViewCategorie(ref List< Categorie> categorieen,String term){
categorieen = categorieen.Where(c => c.omschrijving.ToLower() 。载(term.ToLower()))ToList();
}

我试图使这个搜索方法是通用的,这样我就可以通过列表不同类型的相同方法。我已经尝试了以下内容:

  public static List< object> SearchGridView(List< object> list,String term){
IList< PropertyInfo> properties = list.GetType()。GetProperties()。ToList();
列表< object> tempList = new List< object>();

foreach(列表中的对象o){
foreach(属性中的var属性){
if(property.ToString()。Contains(term.ToLower())){
tempList.Add(o);
}
}
}
return tempList;

$ / code>

然而,使用此解决方案时,我必须将T型列表转换为列表在方法中通过列表之前的对象。



那不是我想要的。我想传递任何类型的List,处理它并返回已作为参数给出的类型的List。这可能吗?

解决方案

我认为这就是您的意思:

 公共静态IList< T> SearchGridView< T>(IList< T>列表,字符串术语)
{
IList< PropertyInfo> properties = typeof(T).GetProperties();
var t = term.ToLower();
return list
.Where(item =>
properties
.Select(p => p.GetValue(item).ToString())
.Any (v => v.Contains(t)))
.ToList();
}


I am trying to build a search function for a List. This is quite simple, however I am trying to have a method, which can take Lists of different types. Take the following example to clearify the question:

The methods SearchGridViewCategorie and SearchGridViewMedewerker both are methods to search the List if it contains the search term.

public static void SearchGridViewMedewerker(ref List<Medewerker> medewerkers, String term) {
    medewerkers = medewerkers.Where(m => m.gebruikersnaam.ToLower().Contains(term.ToLower()) ||
                    m.naam.ToLower().Contains(term.ToLower()) ||
                    m.email.ToLower().Contains(term.ToLower()) ||
                    m.rol.ToLower().Contains(term.ToLower())).ToList();
}

public static void SearchGridViewCategorie(ref List<Categorie> categorieen, String term) {
    categorieen = categorieen.Where(c => c.omschrijving.ToLower().Contains(term.ToLower())).ToList();
}

I am trying to make this search method generic, so that I can pass Lists of different types to the same method. I have tried the following:

public static List<object> SearchGridView(List<object> list, String term) {
    IList<PropertyInfo> properties = list.GetType().GetProperties().ToList();
    List<object> tempList = new List<object>();

        foreach(object o in list){
            foreach (var property in properties) {
                if (property.ToString().Contains(term.ToLower())) {
                    tempList.Add(o);
                }
            }
        }
    return tempList;
}

However with this solution I have to convert the List of Type T to a List of objects prior to passing the List in the method.

That is not what I want. I want to pass a List of any type, process it and return a List of the type that has been given as a parameter. Is this possible?

解决方案

I think that this is what you mean:

public static IList<T> SearchGridView<T>(IList<T> list, String term) 
{
    IList<PropertyInfo> properties = typeof(T).GetProperties();
    var t = term.ToLower();
    return list
        .Where(item =>
            properties
                .Select(p => p.GetValue(item).ToString())
                .Any(v => v.Contains(t)))
        .ToList();
}

这篇关于使用具有不同类型/类别列表的通用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆