微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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))
        }
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐