Note that there are some explanatory texts on larger screens.

plurals
  1. POC# pass a delegate an argument list for a class constructor in a fairly concise way
    primarykey
    data
    text
    <p>In C# can I pass a delegate an argument list for a class constructor in a fairly concise way?</p> <p>I know this is extremely situational and may seem a bit of an odd request, also it's not for the factory pattern. If it helps think of it as a challenge. The correct answer to this question may be that it is not possible.</p> <pre><code>Func&lt;MyClassConstructionArgsType, MyClass&gt; make_MyClass = args =&gt; {return new MyClass(args);}; var n = make_MyClass(comma separated arguments); </code></pre> <p>I also need there to not be a copy of the description of what the arguments are, the below for example is not a solution:</p> <pre><code>Func&lt;int, string, MyClass&gt; make_MyClass = (a, b) =&gt; {return new MyClass(a, b);}; </code></pre> <p>Or, for the same reasons this:</p> <pre><code>Class Args { ... } Func&lt;Args, MyClass&gt; make_MyClass = a =&gt; {return new MyClass(a);}; var n = make_MyClass(Args(args)); </code></pre> <p>Dito where this is the case:</p> <pre><code>var n = make_MyClass&lt;MyClass&gt;(comma separated arguments); </code></pre> <p>The Object[]{comma separated arguments} approach is good except that optional parameters also need to be supported.</p> <p>This question was created as a result of Anastasiosyal's answer from the following question: <a href="https://stackoverflow.com/questions/9365283/c-sharp-class-reference-as-opposed-to-instance-reference/9365373">c# class reference as opposed to instance reference</a></p> <p>Josh first answer seems as close as is possible in C#.</p>
    singulars
    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