簡體   English   中英

在C#中動態設置列表對象類型

[英]Dynamically set list object type in c#

我有一個List對象,我需要它能夠動態交換對象類型。 基本上我有:

List<DataBaseItems> items = new List<DataBaseItems>();

然后,我將使用LINQ在該列表上執行一些過濾,然后綁定到telerik網格。 我需要根據獲得的ID換出對象。 我的目標是建立一個自定義控件,該控件可以將其過濾器按鈕用於多個報表,其中報表數據來自上述列表。 報告A可能使用上面的列表,報告B需要一個完全不同的對象,但對其具有相同的操作。

創建一個接口並將其實現到您實現的任何對象中。

public interface ICustomListFilter<T> 
       where T:class
{
    public void FilterAndBindMyList(List<T> myList);
}

public class ReportOneFilter:ICustomListFilter<MyFirstType>
{
    public void FilterAndBindMyList(List<MyFirstType> items)
    {

        // your filtering and binding code goes here, such as items.Where(i => ...
    }
}

public class ReportTwoFilter:ICustomListFilter<MySecondType>
{
    public void FilterAndBindMyList(List<MySecondType> items)
    {

        // your another filtering and binding algorithm goes here, such as items.Select(i => ...
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM