2

ListForm<T>Windows アプリケーション プロジェクトにフォームがあります。また、そのフォームのようにpredicateフィールドを定義しましたFunc<Order,bool>

private Func<T,bool> predicate;

、、、...ListFormなどのさまざまなタイプで作成できます。OrderDTOCustomerDTODocumentDTO

public class OrderDTO
{
   public string Number {get; set;}
   public string CustomerName {get; set;}
   public int Weight {get; set;}
}

public class CustomerDTO
{
   public int CustomerId {get; set;}
   public string CustomerName {get; set;}
}

タイプpredicateのすべてのプロパティを使用して実行時に動的にビルドする方法はありますか?<T>"X"0ListFormOrderDTOpredicate

t=>t.Number == "X" && t.CustomerName == "X" && t.Weight == 0;

タイプで作成する場合CustomerDTO、述語は

t=> t.CustomerId == 0 && t.CustomerName == "X";

動的検索フォームの作成に使用したい。

実際、ユーザーがフィルター条件をデータに定義できるフィルター選択ウィンドウがListFormあり、このフィルター ウィンドウは、「ISearchable 型に基づく汎用検索フォームの作成」で説明したように動的に作成されます。

4

1 に答える 1

0

以下のように述語を作成します。

private void BuildPredicate()
    {
        predicate = (obj) =>
        {
            var t = typeof(T);
            var strProps = t.GetProperties().Count(p => p.PropertyType == typeof(string) && p.GetValue(obj, null) != "X");
            var intProps = t.GetProperties().Count(p => p.PropertyType == typeof(int) && (int)p.GetValue(obj, null) != 0);
            if (strProps == 0 && intProps == 0)
            {
                return true;
            }
            return false;
        };
    }
于 2013-06-17T10:28:26.773 に答える