0

我在这里有这个不完整且损坏的示例,我想知道是否可以将一个函数传递给另一个考虑模型和属性的函数?请参阅下面示例中的评论以了解我的意思。

private void ValidateSurvey(Survey survey)
{
    // lines 1 to 10, or 1 to 8 or 11 to 16, etc
    for (var i = survey.FirstLine; i <= survey.LastLine; i++)
    {
        IsAnswerValid(survey, "1.1.1", i, /* some rules to apply here i.e. [TModel] != null && [TModel].Value == "foo" */ ) 
    }

}

private bool IsAnswerValid<TAnswer>(
    Survey survey,
    string rowName, 
    int lineNumber,
    Func<TAnswer, bool> answerValueValidator 
) where TAnswer : Answer
{
    if (survey == null)
        throw new ArgumentNullException("survey");
    if (answerValueValidator == null)
        throw new ArgumentNullException("answerValueValidator");
    if (string.IsNullOrWhiteSpace(rowName))
        throw new ArgumentNullException("Null/blank rowName specified");
    if(lineNumber < 0)
        throw new ArgumentOutOfRangeException("lineNumber must be positive");

    // get answer list for the required row
    var answerList = survey.Rows
        .Where(
            r => r.Name.Trim().ToLower() == rowName.Trim().ToLower() && r.Answers != null
        ).SelectMany(a => a.Answers);

    // get the first relevant answer for the line number specified
    var answer = answerList.FirstOrDefault(a => a.LineNumber == lineNumber);

    return answerValueValidator(answer);

    //return (answer != null && answer.Value);
}

更新:根据答案显示 lambda 使用和删除泛型,现在很抱歉只是让自己对导致智能感知不出现的语法错误感到困惑,希望有人会觉得这很有用

private void ValidateSurvey(Survey survey)
{
    // lines 1 to 10, or 1 to 8 or 11 to 16, etc
    for (var i = survey.FirstLine; i <= survey.LastLine; i++)
    {
        IsAnswerValid(survey, "1.1.1", i, a => a != null && a.Value = "foo" ) 
    }

}

private bool IsAnswerValid(
    Survey survey,
    string rowName, 
    int lineNumber,
    Func<Answer, bool> answerValueValidator 
)
{
    if (survey == null)
        throw new ArgumentNullException("survey");
    if (answerValueValidator == null)
        throw new ArgumentNullException("answerValueValidator");
    if (string.IsNullOrWhiteSpace(rowName))
        throw new ArgumentNullException("Null/blank rowName specified");
    if(lineNumber < 0)
        throw new ArgumentOutOfRangeException("lineNumber must be positive");

    // get answer list for the required row
    var answerList = survey.Rows
        .Where(
            r => r.Name.Trim().ToLower() == rowName.Trim().ToLower() && r.Answers != null
        ).SelectMany(a => a.Answers);

    // get the first relevant answer for the line number specified
    var answer = answerList.FirstOrDefault(a => a.LineNumber == lineNumber);

    return answerValueValidator(answer);
}
4

2 回答 2

2

您正在寻找 lambda 表达式:

IsAnswerValid<Answer>(survey, "1.1.1", i, a => a.Something || a.SomethingElse);
于 2013-04-03T02:26:55.650 回答
1

我认为你已经接近你想要的 - 尝试改变这个:

// get the first relevant answer for the line number specified
var answer = answerList.FirstOrDefault(a => a.LineNumber == lineNumber);

对此:

// get the first relevant answer for the line number specified
var answer = answerList
    .OfType<TAnswer>()
    .FirstOrDefault(a => a.LineNumber == lineNumber);

然后你可以传入任何Answer -> boollambda:

IsAnswerValid<Answer>(survey, "1.1.1", i, ans => ans.LineNumber == 1); 
于 2013-04-03T02:32:12.967 回答