Note that there are some explanatory texts on larger screens.

plurals
  1. POCan I accept both a delegate type T and an Expression<T> in the same parameter?
    primarykey
    data
    text
    <p>I'm trying to write a helper class that represents fields on an object. The helper class needs to be able to both get the value of the field on a given instance AND return metadata about the underlying property that it can obtain by reflection.</p> <p>I'd like the helper class be created by a utility method that gets called something like the following:</p> <pre><code>public IEnumerable&lt;IFieldInfo&lt;SomeType&gt;&gt; Fields { get { yield return FieldHelper.GetFieldInfo&lt;SomeType&gt;(t =&gt; t.SomeField); yield return FieldHelper.GetFieldInfo&lt;SomeType&gt;(t =&gt; t.SomeOtherField); } } </code></pre> <p>Say that the <code>IFieldInfo</code> interface looks a bit like this:</p> <pre><code>interface IFieldInfo&lt;in T&gt; { public string PropertyName {get;} public object GetValue(T obj); } </code></pre> <p>In the context of my application, the <code>Fields</code> property will be accessed fairly often; the <code>GetValue()</code> method will be called fairly often, but the <code>PropertyName</code> (and the other metadata/reflectiony fields that I omitted for clarity) will be accessed less frequently. There are a lot of places where the Fields property needs to be implemented, and sometimes a lot of fields, so it's important for code clarity and maintenance that the code that <em>calls</em> <code>GetFieldInfo</code> be simple and clear; duplicating the list of fields or duplicating the parameter would be just <em>asking</em> for bugs where they get out of sync.</p> <p>So, how do I write <code>GetFieldInfo</code>? If I do:</p> <pre><code>public static IFieldInfo&lt;T&gt; GetFieldInfo&lt;T&gt;(Func&lt;T, object&gt; getter); </code></pre> <p>then I can implement <code>GetValue()</code> trivially by calling <code>getter(obj)</code> but I have no way to access the name.</p> <p>If, on the other hand, I do:</p> <pre><code>public static IFieldInfo&lt;T&gt; GetFieldInfo&lt;T&gt;(Expression&lt;Func&lt;T, object&gt;&gt; getter) </code></pre> <p>then I can extract the name of the property access that's represented by the expression, but the only way to implement <code>GetValue()</code> is to call <code>Compile()</code> on the expression, which I imagine to be a ridiculous amount of overhead for something that the compiler itself could compile for me, especially since <code>GetValue()</code> is used more often on these objects than <code>PropertyName</code> is.</p> <p>Is there any way to write my <code>GetFieldInfo</code> helper function to take a single parameter and interpret it as both an expression tree AND a delegate without the overhead of compiling the expression tree into a delegate at runtime?</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.
    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