Extension method for a function
<p>I can create extension methods off any type.
Once such type is Func of int for example.</p>

<p>I want to write extension methods for functions, not the return type of functions.</p>

<p>I can do it in a hacky way:</p>

<pre><code>Func&lt;int&gt; getUserId = () =&gt; GetUserId("Email");
int userId = getUserId.Retry(2);
</code></pre>

<p>Where the function Retry is an extension method defined as:</p>

<pre><code>public static T Retry&lt;T&gt;(this Func&lt;T&gt; func, int maxAttempts)
{
    for (int i = 0; i &lt; maxAttempts; i++)
    {
        try
        {
            return func();
        }
        catch
        {

        }
    }

    throw new Exception("Retries failed.");
}
</code></pre>

<p>What I really want to do is:</p>

<pre><code>var userId = (() =&gt; GetUserId("Email")).Retry(2);
</code></pre>

<p>But the compiler doesn't reconcile the function as a Func of T.</p>

<p>I know of the static including in Roslyn, so I could do something like:</p>

<pre><code>Retry(() =&gt; GetUserId("Email"), 2);
</code></pre>

<p>But I find this harder to read. I really want the helper function I create to be out of the way.</p>

<p>There are other patterns out there that would give me similar results, such as monadic expressions, or using chaining (i.e. convert T to a chain type, that internally has a T, and then I write extension methods for Chain of T). The problem I have with this approach is you have to start off the expression by casting to a Chain of T, and then end the expression by casting to T, which is a lot of noise pulling the reader's attention away from my business logic.</p>

<p>I know I could use implicit casting on Chain of T to T, but this feels like it's doing some magic behind the scenes.</p>

<p>So is it possible to get the reference to a function, without executing it first, with little to no boiler plate code?</p>

<p>End of the day I'd Like to write the following for any kind of Func / Action:</p>

<pre><code>var settings = LoadSettingsFromDatabase().Retry(2);
</code></pre>