将一个函数传递给c#中的另一个函数,以调用给定的模型

本文关键字:函数 另一个 调用 模型 一个 | 更新日期: 2023-09-27 18:07:04

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

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("answerswerValueValidator");
    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("answerswerValueValidator");
    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);
}

将一个函数传递给c#中的另一个函数,以调用给定的模型

您正在寻找lambda表达式:

IsAnswerValid<Answer>(survey, "1.1.1", i, a => a.Something || a.SomethingElse);

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

// 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 -> bool lambda:

IsAnswerValid<Answer>(survey, "1.1.1", i, ans => ans.LineNumber == 1);