Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there any way to infer an Action type, or a full Action?
    text
    copied!<p>I find myself (too) often using a construct like the following:</p> <pre><code>class MyClass { public TypeA ObjectA; public TypeB ObjectB; public TypeC ObjectC; public List&lt;TypeD&gt; ListOfObjectD = new List&lt;TypeD&gt;(); public void DoSmth() { return SomeConstruct( /*...*/ new Setter&lt;TypeA&gt;(a =&gt; ObjectA = a), // these are the new Setter&lt;TypeB&gt;(b =&gt; ObjectB = b), // things I'm trying new Setter&lt;TypeC&gt;(c =&gt; ObjectC = c), // to make shorter new Setter&lt;TypeD&gt;(d =&gt; ListOfObjectD.Add(d)), /*...*/ ); } } class Setter&lt;T&gt; { public Action&lt;T&gt; Action; public Setter(Action&lt;T&gt; action) { Action = action; } } </code></pre> <p>Is there any way for the Setter class to infer the type of the Action and create the standard <code>(T obj) =&gt; Member = obj</code> Action by only passing the Member in some way? I'm thinking of something like:</p> <pre><code>new Setter(ObjectA) </code></pre> <p>which of course is not valid syntax, but should give you an idea what I'm trying to achieve. I'm using this construct literally <strong>hundreds</strong> of time in my code, so the code saved by this small change would be tremendous.</p> <hr> <p><strong>Edit:</strong> Added the TypeD example. The part</p> <pre><code>new Setter&lt;TypeD&gt;(d =&gt; ListOfObjectD.Add(d)) </code></pre> <p>can be simplified to</p> <pre><code>new Setter&lt;TypeD&gt;(ListOfObjectD.Add) </code></pre> <p>which is awesome because it cuts from the redundant code. If only <code>&lt;TypeD&gt;</code> could also be inferred it would be perfect. I'm looking for something like this for the others.</p> <p><strong>@Lazarus</strong> - basically the purpose is to return setters, so other objects can set certain members of the class (or it can do other stuff defined in the Action) without accessing the class itself, only the Setter object. The full list of reasons is long and convoluted, but the structuring of the program works like a charm and I doubt needs changing (the example of course is simplified and doesn't really make sense as is).</p> <hr> <p><strong>Edit 2:</strong> I found a good way to simplify things for List's:</p> <pre><code>static class SetterHelper { public static Setter&lt;T&gt; GetSetter&lt;T&gt;(this List&lt;T&gt; list) { return new Setter&lt;T&gt;(list.Add); } } </code></pre> <p>Now I can just use this:</p> <pre><code>ListOfObjectD.GetSetter() </code></pre> <p>which works perfectly! why can't I do the same for T directly? I tried this:</p> <pre><code>static class SetterHelper { public static Setter&lt;T&gt; GetSetter&lt;T&gt;(this T item) { return new Setter&lt;T&gt;(t =&gt; item = t); // THIS DOESN'T SET THE PASSED MEMBER } } </code></pre> <p>Of course it won't work as intended because it will set item, but not the passed member. I tried adding <code>ref</code> as <code>(ref this T item)</code> but it won't compile :(... It would have been perfect.</p>
 

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