Note that there are some explanatory texts on larger screens.

plurals
  1. POPass a method into another method that requires the method name
    primarykey
    data
    text
    <p>Below is some code that is used to pass around a reference to a method that contains strings as parameters, the purpose of this question is around using generics to negate the need to define the actual type!</p> <pre><code>Impossible&lt;ExampleSource, string&gt;.Example(c =&gt; c.NonString); //does not work Impossible&lt;ExampleSource, string&gt;.Example&lt;int&gt;(c =&gt; c.NonString); //does work </code></pre> <p><strong>The idea here is to make the first "NonString" call work without having to define the parameter type or declare a new function in Impossible that takes Func&lt;int, TResult&gt;.</strong></p> <pre><code>public static void Example(Expression&lt;Func&lt;TSource, Func&lt;int, TResult&gt;&gt;&gt; function) { Process(function as MethodCallExpression); } //invalid solution... </code></pre> <p><strong>In Java this could be achieved using Func&lt;?, TResult&gt;</strong></p> <pre><code>public class Impossible&lt;TSource, TResult&gt; { public static void Example(Expression&lt;Func&lt;TSource, Func&lt;TResult&gt;&gt;&gt; function) { Process(function as MethodCallExpression); } public static void Example(Expression&lt;Func&lt;TSource, Func&lt;string, TResult&gt;&gt;&gt; function) { Process(function as MethodCallExpression); } public static void Example(Expression&lt;Func&lt;TSource, Func&lt;string, string, TResult&gt;&gt;&gt; function) { Process(function as MethodCallExpression); } public static void Example&lt;T1&gt;(Expression&lt;Func&lt;TSource, Func&lt;T1, TResult&gt;&gt;&gt; function) { Process(function as MethodCallExpression); } public static void Example&lt;T1, T2&gt;(Expression&lt;Func&lt;TSource, Func&lt;T1, T2, TResult&gt;&gt;&gt; function) { Process(function as MethodCallExpression); } private static void Process(MethodCallExpression exp) { if (exp == null) return; Console.WriteLine(exp.Method.Name); } } public class ExampleSource { public string NoParams() { return ""; } public string OneParam(string one) { return ""; } public string TwoParams(string one, string two) { return ""; } public string NonString(int i) { return ""; } } public class Consumer { public void Argh() { Impossible&lt;ExampleSource, string&gt;.Example(c =&gt; c.NoParams); Impossible&lt;ExampleSource, string&gt;.Example(c =&gt; c.OneParam); Impossible&lt;ExampleSource, string&gt;.Example(c =&gt; c.TwoParams); Impossible&lt;ExampleSource, string&gt;.Example&lt;int&gt;(c =&gt; c.NonString); Impossible&lt;ExampleSource, string&gt;.Example(c =&gt; c.NonString); //MAKE THIS WORK } } </code></pre>
    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