get方法作为参数

时间:2014-07-31 06:02:20

标签: c# delegates

我是C#的新手。

我想查看一个元素,因为我有一个Reviewer类和一个Checklist(静态)类。

我希望有一个功能Reviewer Validate(element)。

此函数应从清单(以委托或其他方式的形式)获取审阅逻辑,并通过仅使用该逻辑上的元素来检查元素。

因为物理清单只有逻辑,审阅者(人)在元素上使用这些逻辑

注意:我不希望审阅者调用Checklist类中的函数来验证元素。

这在C#中是否可行?

我需要类似下面的内容。

static class Checklist
{
}

static class Reviewer
{
public Result Valiadate(Element element)
{
////i want to do here wat is linq doing internally 
//// i.e lync gets the logic from us , performs it on the object.
/// similarly i have the object here as element , need to get logic from checklist and review the element using it.
}
}

Class Client
{
void Validate(Element element)
{
Reviewer.Validate(element);
}
}

1 个答案:

答案 0 :(得分:1)

你的问题似乎很奇怪。你想要这样的东西吗?

public static class Checklist
{
    public static List<Reviewer> Reviewers { get; private set; }

    public static bool Validate(object element)
    {
        return true;
    }

    static Checklist()
    {
        Reviewers = new List<Reviewer>();
        Reviewers.Add(new Reviewer((elem)=>true));
    }
}

public class Reviewer
{
    private Func<object, bool> _itsValidator;

    public Reviewer(Func<object,bool> validator)
    {
        _itsValidator = validator;
    }

    public bool Validate(object element)
    {
        return _itsValidator(element);
    }
}