Note that there are some explanatory texts on larger screens.

plurals
  1. POpass properties as reference using Expressions
    primarykey
    data
    text
    <p><a href="https://stackoverflow.com/questions/1402803/passing-properties-by-reference-in-c">This</a> post details workarounds for passing properties as references including using Expressions such as </p> <pre><code> public void StoreProperty(Expression&lt;Func&lt;T, object&gt;&gt; expr) </code></pre> <p>This approach is ok and I note many frameworks appear to use this (eg automapper, autofac) as detailed in James Gregory's <a href="http://jagregory.com/writings/introduction-to-static-reflection/" rel="nofollow noreferrer">Introduction to static reflection</a> where he states </p> <blockquote> <p><em>The great thing about this is that if you change the name of a member inside a lambda, you’ll get a compile error if you haven’t updated all the references! No more hidden bugs.</em></p> </blockquote> <p>Whilst I much prefer this approach it is still not perfect as you can pass any expression returning an object (or whatever your return val is) eg </p> <pre><code> x =&gt; x.Name) //fine x =&gt; x.Name+"x") //runtime error </code></pre> <p>Is there currently any better way to reference the property (by locking down the Expression, or some other way)</p> <p>If No, how might a future version of C# lock down the Expression? for example, something like:</p> <pre><code>public void StoreProperty(Expression&lt;Func&lt;T, object&gt;&gt; expr) where expr.Member is PropertyInfo </code></pre> <p><strong>clarification</strong>: above is only an example, I know this isn't currently supported; thats what I'm trying to discuss.</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