Note that there are some explanatory texts on larger screens.

plurals
  1. POGeneric Func<> as parameter to base method
    primarykey
    data
    text
    <p>I might be losing the plot, but I hope someone can point me in the right direction.</p> <p>What am I trying to do?</p> <p>I'm trying to write some base methods which take Func&lt;> 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.</p> <p>So far this is the base class.</p> <pre><code>public abstract class ServiceBase&lt;T&gt; { 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&lt;T1, T2, T3, TResult&gt;(Func&lt;T1, T2, T3, TResult&gt; function) { try { /* what goes here?! */ } catch (Exception exception) { throw new Exception(exception.Message); } } } </code></pre> <p>I want to execute an Action in the following way in the derived class (this seems to work):</p> <pre><code>public void Delete(string application, string key) { ExecAction(() =&gt; Settings.Delete(application, key)); } </code></pre> <p>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.</p> <p>I want to be able to call it in the following way (if possible):</p> <pre><code>public object Get(string application, string key, int? expiration) { return ExecFunc(() =&gt; Settings.Get(application, key, expiration)); } </code></pre> <p>Am I thinking too crazy or is this possible? Thanks in advance for all the help.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload