通用Func<>作为基本方法的参数 [英] Generic Func<> as parameter to base method

查看:98
本文介绍了通用Func<>作为基本方法的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能会失去这个阴谋,但我希望有人能指出我正确的方向。



我在做什么?



我试图编写一些采用Func& Action和Action的基本方法,以便这些方法处理所有的异常处理等,所以它不会在整个地方重复,而是允许派生类指定要执行的操作。



到目前为止,这是基类。

  public abstract class ServiceBase< T> 
{
保护T设置{get;组; }

受保护的ServiceBase(T设置)
{
设置=设置;

$ b $ public void ExecAction(Action action)
{
try
{
action();
}
catch(异常异常)
{
抛出新异常(exception.Message);


$ b $ public TResult ExecFunc< T1,T2,T3,TResult>(Func< T1,T2,T3,TResult> function)
{
试试
{
/ *这里有什么? * /
}
catch(异常异常)
{
抛出新异常(exception.Message);



$ / code $ / pre

我想执行一个Action在派生类中使用以下方法(这似乎工作):

  public void Delete(string application,string key)
{
ExecAction(()=> Settings.Delete(application,key));
}

我想在派生类中以类似的方式执行Func,但对于我而言,我似乎无法制定将基类放入什么内容。



我希望能够通过以下方式调用它(如果可能的话):

  public object Get(string application,string key,int?expiration)
{
return ExecFunc(()=> Settings.Get(application,key,expiration));
}

我是不是觉得太疯狂了?提前感谢所有的帮助。

解决方案

  public void Delete(string application,string Key)
{
ExecAction(()=> Settings.Delete(application,key));


public Object get(string application,string key,int?expiration)
{
return ExecFunc(()=> Settings.Get(application,密钥,到期));

$ b $ ...

public TResult ExecFunc< TResult>(Func< TResult> func)
{
try
{
return func();
}
catch(异常异常)
{
抛出新异常(exception.Message);






$ b

顺便说一句,你的异常处理看起来有点不礼貌:首先,捕获 Exception 本身并不是一个好习惯。请考虑捕获更具体的异常。其次,你在 catch 块中抛出一个新的异常,这意味着你正在失去原始异常的堆栈跟踪等。您应该使用 throw; 来替代重新抛出原始异常。 (这假定你的 catch 块正在做一些有用的工作,如果你所做的只是捕获并抛出,那么就放弃 try。 ..catch 完全阻止。)


I might be losing the plot, but I hope someone can point me in the right direction.

What am I trying to do?

I'm trying to write some base methods which take Func<> and Action so that these methods handle all of the exception handling etc. so its not repeated all over the place but allow the derived classes to specify what actions it wants to execute.

So far this is the base class.

public abstract class ServiceBase<T>
{
    protected T Settings { get; set; }

    protected ServiceBase(T setting)
    {
        Settings = setting;
    }

    public void ExecAction(Action action)
    {
        try
        {
            action();
        }
        catch (Exception exception)
        {
            throw new Exception(exception.Message);
        }
    }

    public TResult ExecFunc<T1, T2, T3, TResult>(Func<T1, T2, T3, TResult> function)
    {
        try
        {
            /* what goes here?! */
        }
        catch (Exception exception)
        {
            throw new Exception(exception.Message);
        }
    }
}

I want to execute an Action in the following way in the derived class (this seems to work):

public void Delete(string application, string key)
{
  ExecAction(() => Settings.Delete(application, key));
}

And I want to execute a Func in a similar way in the derived class but for the life of me I can't seem to workout what to put in the base class.

I want to be able to call it in the following way (if possible):

public object Get(string application, string key, int? expiration)
{
  return ExecFunc(() => Settings.Get(application, key, expiration));
}

Am I thinking too crazy or is this possible? Thanks in advance for all the help.

解决方案

public void Delete(string application, string key)
{
    ExecAction(() => Settings.Delete(application, key));
}

public object Get(string application, string key, int? expiration)
{
    return ExecFunc(() => Settings.Get(application, key, expiration));
}

// ...

public TResult ExecFunc<TResult>(Func<TResult> func)
{
    try
    {
        return func();
    }
    catch (Exception exception)
    {
        throw new Exception(exception.Message);
    }
}

By the way, your exception handling looks a bit dodgy: Firstly, it's not considered good practice to catch Exception itself. Consider catching more specific exceptions instead. Secondly, you're throwing a new exception in your catch block, which means that you're losing the stacktrace etc from the original exception. You should use throw; instead to rethrow the original exception. (This assumes that your catch block is doing some sort of useful work. If all you're doing is catching and throwing then just ditch the try...catch blocks altogether.)

这篇关于通用Func&lt;&gt;作为基本方法的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆