5

我的第一个(也是非常可怕的帖子)在下面。

我尝试做一个完整的例子,我想得到什么。我希望这将得到更好的解释。

using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Boy> boys = new List<Boy>();
            boys.Add(new Boy("Jhon", 7));
            boys.Add(new Boy("Oscar", 6));
            boys.Add(new Boy("Oscar", 7));
            boys.Add(new Boy("Peter", 5));
            ClassRoom myClass = new ClassRoom(boys);

            Console.WriteLine(myClass.ByName("Oscar").Count);  // Prints 2
            Console.WriteLine(myClass.ByYearsOld(7).Count);  // Prints 2

            // This has errors...................
            // But this is as I would like to call my BySomeConditions method....
            Console.WriteLine(  // It should print 1
                                myClass.BySomeConditions([myClass.ByName("Oscar"),
                                                          myClass.ByYearsOld(7)]
                                                        )
                             );
            Console.ReadKey();
        }





        class ClassRoom
        {
            private List<Boy> students;

            public ClassRoom(List<Boy> students)
            {
                this.students = students;
            }

            public List<Boy> ByName(string name)
            {
                return students.FindAll(x => x.Name == name);
            }
            public List<Boy> ByYearsOld(int yearsOld)
            {
                return students.FindAll(x => x.YearsOld == yearsOld);
            }

            // This has ERRORS.......................
            public List<Boy> BySomeConditions(params Func<X, List<Boy>>[] conditions)
            {
                IEnumerable<Boy> result = students;                
                foreach (var condition in conditions) {
                    // I want it ONLY be called with existent functions (ByName and/or ByYearsOld)
                    result = result.Intersect(condition(this));  
                }
            }
        }


        class Boy
        {
            public string Name { get; set; }
            public int YearsOld { get; set; }
            public Boy(string name, int yearsOld)
            {
                Name = name;
                YearsOld = yearsOld;
            }
        }
    }
}

============== 第一篇===================== 你好,

我有一个方法类:

public class X
{
    private readonly List<string> myList;

    public X(List<string> paramList) // string is really an object
    {
         myList = paramList;
    }

    // Now I want this...
    public List<string> CheckConditions(params Func<T, List<string>>[] conditions)
    {
         var result = myList;
         foreach (Func<T, List<string>> condition in conditions)
         {
               result = result.Intersect(condition(T));
         }
    }

    public List<string> check1(string S)
    {
         return myList.FindAll(x => x.FieldS == S);
    }
    public List<string> check1(int I)
    {
         return myList.FindAll(x => x.FieldI == I);
    }
}

对不起,如果有一些错误,我从头开始写,以避免复杂的真实情况。

我想要的是这样调用我的方法:

   X.check1("Jhon");

或者

   X.check2(12);

或(这是我的问题的目标):

   X.CheckConditions(X.check1("Jhon"), X.chek2(12));

感谢和对不起我的可怜的例子......

4

5 回答 5

3

目前尚不清楚您的 T 来自哪里。

这符合您的要求吗?

public class X<T>
{
  private List<T> myList;

  public List<T> CheckConditions(params Func<T, bool>[] conditions)
  {
    IEnumerable<T> query = myList;
    foreach (Func<T, bool> condition in conditions)
    {
      query = query.Where(condition);
    }
    return query.ToList();
  }
}

然后后来:

List<T> result = X.CheckConditions(
  z => z.FieldS == "Jhon",
  z => z.FieldI == 12
);
于 2011-03-11T20:54:16.337 回答
2

您需要更改 的方法签名CheckConditions,它接受可变数量的List<string>,而不是函数。

public List<string> CheckConditions(params List<string>[] lists)

的返回类型check1List<string>,所以它需要是CheckConditions接受的参数的类型。

于 2011-03-11T20:44:33.423 回答
1

没有理由让它通用,你知道你想对当前实例进行操作X(所以传入this,而不是T类型参数)。您需要清理一些东西以使其编译(返回result并使类型resultIntersect调用兼容)。你可以这样定义它:

public List<string> CheckConditions(params Func<X, List<string>>[] conditions)
{
     IEnumerable<string> result = myList;
     foreach (var condition in conditions)
     {
           result = result.Intersect(condition(this));
     }

     return result.ToList();
}

Ant 然后这样称呼它:

xInstance.CheckConditions(x => x.check1("JHon"), x => x.check1(12));

说了这么多,我不知道你为什么不只是传递这些函数的结果,而不是传递实际的函数:

public List<string> CheckConditions(params List<string>[] conditions)
{
     IEnumerable<string> result = myList;
     foreach (var condition in conditions)
     {
           result = result.Intersect(condition);
     }

     return result.ToList();
}

然后像在您的示例中那样调用它,而不是传入 lambda 表达式。

于 2011-03-11T20:49:34.260 回答
0

你传递给你的东西

X.CheckConditions

不是对函数的引用,而是它们调用的返回值。

现在,如果你传递函数引用 - 它不带有参数,除非你构造并传递一个包含函数引用和它应该处理的参数的数据结构。

在这种情况下 - 泛型不是解决方案。您应该考虑遵循另一种模式,例如命令模式或策略模式,在其中您传递给CheckConstruction检查器对象的实例,每个实例都使用它应该处理的参数进行实例化,并且实现或由验证函数提供。

于 2011-03-11T20:44:28.320 回答
0

你可以重写你的函数看起来像这样:

// Now I want this...
    public List<string> CheckConditions(params Func<T, List<string>>[] conditions)
    {
         var result = myList;
         foreach (Func<T, List<string>> condition in conditions)
         {
               result = result.Intersect(condition(T));
         }
    }

你的电话将是X.CheckConditions(()=>X.check1("Jhon"), ()=>X.chek2(12));

并且您需要为 x 提供一个实例(因为方法是实例方法而不是静态方法)

在您的示例中,您将 T 作为参数传递给仿函数,但 T 是类型参数,因此它不能作为参数传递给方法。你的意思是传递一个值吗?

这要求澄清您为什么要这样做。也许如果您提供了有关您要完成的工作的详细信息(而不是如何完成),那么您可以获得更好的问题解决方案。

于 2011-03-11T20:47:16.107 回答