Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a way to avoid overloading a method with Func<T, ...> argument for each Func parameter count
    primarykey
    data
    text
    <p>I know the question sounds a bit wierd. Sorry for that, having difficulties trying to get where i want to and even explain it. To make it simple, i have a method with <code>Func&lt;T&gt;</code> argument. But i do not always pass a parameterless <em>Action</em> to that method, i need <strong>varying</strong> numbers of parameters and i'm trying to find a way to avoid <em>overloading</em> my method everytime number of parameters needed is increased</p> <p>Here is my <em>generic</em> class, I need to <em>overload</em> <code>GetInstance</code> method:</p> <pre><code>public class MethodResult&lt;T&gt; { public T Result { get; set; } public bool IsResulted { get; set; } public Exception Error { get; set; } private MethodResult() { } public static MethodResult&lt;T&gt; GetInstance&lt;T&gt;(Func&lt;T&gt; method) { MethodResult&lt;T&gt; obj = new MethodResult&lt;T&gt;(); try { obj.Result = method(); obj.IsResulted = true; obj.Error = null; } catch (Exception ex) { obj.Result = default(T); obj.IsResulted = false; obj.Error = ex; } return obj; } public static MethodResult&lt;T&gt; GetInstance&lt;T, T1&gt;(Func&lt;T1, T&gt; method, T1 param1) { MethodResult&lt;T&gt; obj = new MethodResult&lt;T&gt;(); try { obj.Result = method(param1); obj.IsResulted = true; obj.Error = null; } catch (Exception ex) { obj.Result = default(T); obj.IsResulted = false; obj.Error = ex; } return obj; } } </code></pre> <p>And here is sample showing how i want to make use of it:</p> <pre><code>public static void Main(string[] args) { var x = MethodResult&lt;int&gt;.GetInstance(IntResult, 5); Console.WriteLine("Result: {0}, IsResulted: {1}, ErrorMessage: {2}", x.Result, x.IsResulted, (x.Error == null ? "null" : x.Error.Message)); var y = MethodResult&lt;string&gt;.GetInstance(SayHello); Console.WriteLine("Result: {0}, IsResulted: {1}, ErrorMessage: {2}", y.Result, y.IsResulted, (y.Error == null ? "null" : y.Error.Message)); Console.Read(); } public static int IntResult(int x) { return x + 1; } public static int IntResult(int x, int y) { return x + y; } public static string SayHello() { return "Hello world!"; } </code></pre> <p>In order to be able to use <code>IntResult(int x, int y)</code> i have to <em>overload</em> <code>GetInstance</code> method with signiture:</p> <p><code>public static MethodResult&lt;T&gt; GetInstance&lt;T, T1, T2&gt;(Func&lt;T1, T2, T&gt; method, T1 param1, T2 param2)</code></p> <p>It's obvious that this will become very time consuming as it's already been annoying. Is there a way to avoid that much <em>overloading</em> ?</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