asp.net – 如何使用返回类型的System.Action?

在BLL课程中,我写道:

Private List<T> GetData(string a,string b)
{
   TryAction(()=>{
      //Call BLL Method to retrieve the list of BO.
       return BLLInstance.GetAllList(a,b);
    });
}

在BLL基类中,我有一个方法

protected void TryAction(Action action)
{
 try
 {
   action();
 }
 catch(Exception e)
 {
   // write exception to output (Response.Write(str))
 }
}

如何将TryAction()方法与泛型返回类型一起使用?
请有个建议.

解决方法

您需要使用Func来表示将返回值的方法.

以下是一个例子

private List<int> GetData(string a,string b)
    {
        return TryAction(() =>
        {
            //Call BLL Method to retrieve the list of BO.
            return BLLInstance.GetAllList(a,b);
        });
    }


    protected TResult TryAction<TResult>(Func<TResult> action)
    {
        try
        {
            return action();
        }
        catch (Exception e)
        {
            throw;
            // write exception to output (Response.Write(str))
        }
    }

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....