C# 如何将函数传递给方法?

C# 如何将函数传递给方法?,c#,asp.net-mvc-3,C#,Asp.net Mvc 3,在我的控制器中,我总是以如下方式结束: [HttpPost] public ActionResult General(GeneralSettingsInfo model) { try { if (ModelState.IsValid) { // Upload database db.UpdateSettingsGeneral(model, currentUser.UserId);

在我的控制器中,我总是以如下方式结束:

[HttpPost]
public ActionResult General(GeneralSettingsInfo model)
{
    try
    {
        if (ModelState.IsValid)
        {
            // Upload database
            db.UpdateSettingsGeneral(model, currentUser.UserId);
            this.GlobalErrorMessage.Type = ErrorMessageToViewType.success;
        }
        else
        {
            this.GlobalErrorMessage.Type = ErrorMessageToViewType.alert;
            this.GlobalErrorMessage.Message = "Invalid data, please try again.";
        }
    }
    catch (Exception ex)
    {
        if (ex.InnerException != null)
            while (ex.InnerException != null)
                ex = ex.InnerException;

        this.GlobalErrorMessage.Type = ErrorMessageToViewType.error;
        this.GlobalErrorMessage.Message = this.ParseExceptionMessage(ex.Message);
    }

    this.GlobalErrorMessage.ShowInView = true;
    TempData["Post-data"] = this.GlobalErrorMessage;

    return RedirectToAction("General");
}
[HttpPost]
public ActionResult General(GeneralSettingsInfo model)
{
    saveModelIntoDatabase(
        ModelState, 
        db.UpdateSettingsGeneral(model, currentUser.UserId)
    );

    return RedirectToAction("General");
}
我想做的是:

[HttpPost]
public ActionResult General(GeneralSettingsInfo model)
{
    try
    {
        if (ModelState.IsValid)
        {
            // Upload database
            db.UpdateSettingsGeneral(model, currentUser.UserId);
            this.GlobalErrorMessage.Type = ErrorMessageToViewType.success;
        }
        else
        {
            this.GlobalErrorMessage.Type = ErrorMessageToViewType.alert;
            this.GlobalErrorMessage.Message = "Invalid data, please try again.";
        }
    }
    catch (Exception ex)
    {
        if (ex.InnerException != null)
            while (ex.InnerException != null)
                ex = ex.InnerException;

        this.GlobalErrorMessage.Type = ErrorMessageToViewType.error;
        this.GlobalErrorMessage.Message = this.ParseExceptionMessage(ex.Message);
    }

    this.GlobalErrorMessage.ShowInView = true;
    TempData["Post-data"] = this.GlobalErrorMessage;

    return RedirectToAction("General");
}
[HttpPost]
public ActionResult General(GeneralSettingsInfo model)
{
    saveModelIntoDatabase(
        ModelState, 
        db.UpdateSettingsGeneral(model, currentUser.UserId)
    );

    return RedirectToAction("General");
}
如何将函数作为参数传递?就像我们在javascript中所做的那样:

saveModelIntoDatabase(ModelState, function() {
    db.UpdateSettingsGeneral(model, currentUser.UserId)
});
听起来你需要一名代表。我现在还不清楚您的委托类型应该是什么-可能只是
操作

SaveModelIntoDatabase(ModelState, 
    () => db.UpdateSettingsGeneral(model, currentUser.UserId));
其中
SaveModelIntoDatabase
将是:

public void SaveModelIntoDatabase(ModelState state, Action action)
{
    // Do stuff...

    // Call the action
    action();
}
如果希望函数返回某些内容,请使用
Func
;如果您需要额外的参数,只需将它们作为类型参数添加即可-有
Action
Action
Action

如果您是新学员,我强烈建议您在进一步学习C#之前要更加熟悉它们-它们非常方便,是现代惯用C#的重要组成部分。网上有很多关于他们的信息,包括:


操作myFunction
委派
操作
Func
我使用委派的唯一时间是在Windows应用程序下处理事件。。。我从来没有想到我可以在ASP.NET中轻松地做到这一点:/。。。我会先读一读,谢谢你指出。