C# 将 Func<T, List<myClass>> 数组传递给方法

fer*_*ega 5 .net c# generics func

我的第一篇(也是非常可怕的帖子)如下。

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

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;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

============== 第一篇文章 ===================== 你好,

我有一个带有方法的类:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

抱歉,如果有一些错误,我是从头开始编写的,以避免复杂的实际情况。

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

   X.check1("Jhon");
Run Code Online (Sandbox Code Playgroud)

或者

   X.check2(12);
Run Code Online (Sandbox Code Playgroud)

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

   X.CheckConditions(X.check1("Jhon"), X.chek2(12));
Run Code Online (Sandbox Code Playgroud)

感谢并抱歉我的糟糕例子......

Rad*_*own 0

你传递给你的

X.CheckConditions
Run Code Online (Sandbox Code Playgroud)

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

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

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