Note that there are some explanatory texts on larger screens.

plurals
  1. POCreate a constructor call using Reflection Emit that passes a Func<> as a parameter
    primarykey
    data
    text
    <p>I'm hoping someone can point me in the right direction with the following problem. </p> <p>I am working on a project where the types are generated using Reflection.Emit, all has been working fine until a requirement arose where I needed to pass a Func&lt;> into a constructor of a new object as below.</p> <pre><code>public class SearchTerm : IEntity { private readonly NavigationProperty&lt;Item&gt; _item; public SearchTerm() { _item = new NavigationProperty&lt;Item&gt;(() =&gt; ItemIds); } public string[] ItemIds { get; set; } } </code></pre> <p>Using Linqpad I can see the IL output is as follows:</p> <pre><code>SearchTerm.&lt;.ctor&gt;b__0: IL_0000: ldarg.0 IL_0001: call UserQuery+SearchTerm.get_ItemIds IL_0006: stloc.0 // CS$1$0000 IL_0007: br.s IL_0009 IL_0009: ldloc.0 // CS$1$0000 IL_000A: ret SearchTerm..ctor: IL_0000: ldnull IL_0001: stloc.0 IL_0002: ldarg.0 IL_0003: call System.Object..ctor IL_0008: nop IL_0009: nop IL_000A: ldarg.0 IL_000B: ldloc.0 IL_000C: brtrue.s IL_001D IL_000E: ldarg.0 IL_000F: ldftn UserQuery+SearchTerm.&lt;.ctor&gt;b__0 IL_0015: newobj System.Func&lt;System.Collections.Generic.IEnumerable&lt;System.String&gt;&gt;..ctor IL_001A: stloc.0 IL_001B: br.s IL_001D IL_001D: ldloc.0 IL_001E: newobj UserQuery&lt;UserQuery+Item&gt;+NavigationProperty`1..ctor IL_0023: stfld UserQuery+SearchTerm._item IL_0028: nop IL_0029: ret </code></pre> <p>The problem I have is that I'm not sure how to define the delegate within IL.</p> <p>I have tried the following</p> <pre><code>var method = typeBuilder.DefineMethod("func", MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig | MethodAttributes.Virtual, typeof(IEnumerable&lt;string&gt;), Type.EmptyTypes); var methodIl = method.GetILGenerator(); methodIl.Emit(OpCodes.Ldarg_0); methodIl.Emit(OpCodes.Call, dictionary["get_ItemIds"]); methodIl.Emit(OpCodes.Ret); </code></pre> <p>Then trying to create the delegate the following throws "Not Supported Exception" with the error "Derived classes must provide an implementation."</p> <pre><code>var funcType = typeof(Func&lt;,&gt;).MakeGenericType(typeBuilder, typeof(IEnumerable&lt;string&gt;)); method.CreateDelegate(funcType); </code></pre> <p>I have also tried using Delegate.CreateDelegate and DynamicMethod which both require the type to have been created before using them.</p> <p>Any suggestion on what I might be doing wrong are greatly appreciated.</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.
    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