Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could of course walk the expression-tree and then use <code>Delegate.CreateDelegate</code> to create the appropriate <code>Action&lt;,&gt;</code>. It's quite simple, except for all of the validation-checks (I'm unsure if I've covered everything):</p> <p><s>I'm no expression-tree expert, but I don't <em>think</em> building an expression-tree and then calling <code>Compile</code> is possible here since expression-trees can't contain assignment statements, as far as I know.</s> (<strong>EDIT</strong>: Apparently, these have been added in .NET 4. It's a hard-to-find feature since the C# compiler doesn't seem to be able to build them from lambdas).</p> <pre><code>public static Action&lt;TContaining, TProperty&gt; CreateSetter&lt;TContaining, TProperty&gt; (Expression&lt;Func&lt;TContaining, TProperty&gt;&gt; getter) { if (getter == null) throw new ArgumentNullException("getter"); var memberEx = getter.Body as MemberExpression; if (memberEx == null) throw new ArgumentException("Body is not a member-expression."); var property = memberEx.Member as PropertyInfo; if (property == null) throw new ArgumentException("Member is not a property."); if(!property.CanWrite) throw new ArgumentException("Property is not writable."); return (Action&lt;TContaining, TProperty&gt;) Delegate.CreateDelegate(typeof(Action&lt;TContaining, TProperty&gt;), property.GetSetMethod()); } </code></pre> <p><strong>Usage</strong>:</p> <pre><code>public class Person { public int Age { get; set; } } ... static void Main(string[] args) { var setter = CreateSetter((Person p) =&gt; p.Age); var person = new Person(); setter(person, 25); Console.WriteLine(person.Age); // 25 } </code></pre> <p>Do note that this creates an <em>open</em> instance delegate, meaning that it's not bound to any particular instance of <code>TContaining</code>. It's simple to modify it to be bound to a <em>specific</em> instance; you'll have to pass a <code>TContaining</code> as well to the method and then use a different overload of <code>Delegate.CreateDelegate</code>. The signature of the method would then look something like:</p> <pre><code>public static Action&lt;TProperty&gt; CreateSetter&lt;TContaining, TProperty&gt; (Expression&lt;Func&lt;TContaining, TProperty&gt;&gt; getter, TContaining obj) </code></pre>
    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.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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