title: Retrieving a delegate type using custom types from an assembly that is currently being built
body: <p>I am trying to make a compiler in F# using the API provided in System.Reflection.Emit. I am running into a problem when I try to create functions (delegates) for the types in the assembly that I am currently building. Example translating the following Java (a slightly modified version of Java) class:</p>
  <pre><code>class A extends Object {
      Integer x;
      A (Integer x) {
          super();
          this.x = x;
      }
      Function&lt;A, Integer&gt; fun() {
          return (A a) -&gt; { return a.x; };
      }
  }
  </code></pre>
  <p>So, I need to be able to use the System.Type of Func&lt;A, Integer&gt; in order to generate the following CIL instruction:</p>
  <pre><code>newobj instance void class [mscorlib]System.Func`2&lt;class tst.A,int32&gt;::.ctor(object, native int) 
  </code></pre>
  <p>The code generating the respective instruction is:</p>
  <pre><code>let deleg = System.Linq.Expressions.Expression.GetDelegateType(types)
  let constr = deleg.GetConstructor([|typeof&lt;obj&gt; ; typeof&lt;nativeint&gt;|])
  ilGenerator.Emit(OpCodes.Newobj, constr) 
  </code></pre>
  <p>where <code>types</code> is an array of System.Type elements. In case of Fun&lt;A, Integer&gt;, the types array is <code>[|typ ; typeof&lt;int&gt;|]</code>, where typ is declared like: <code>let typ : System.Type = upcast typeBuilder</code> . However, this triggers the following  error:</p>
  <pre><code>System.NotSupportedException: Specified method is not supported.
  at System.Reflection.Emit.TypeBuilderInstantiation.GetConstructorImpl(BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
  at System.Type.GetConstructor(BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers)
  </code></pre>
  <p>If I needed to generate the same instruction for a function that uses .NET types, for instance Func&lt;Integer, Integer&gt;, then the above code works perfectly (the types array is <code>types = [| typeof&lt;int&gt; ; typeof&lt;int&gt; |]</code> in this case). Any suggestions why the above solution doesn't work or any other alternatives of how to retrieve the System.Type of a TypeBuilder (that is not finished building) are more than welcome.</p>
  <p>The way the typeBuilder for the class A is generated is:</p>
  <pre><code>let typeBuilder = moduleBuilder.DefineType(typ, TypeAttributes.Public ||| TypeAttributes.Class)
  typeBuilder.SetParent(typeof&lt;obj&gt;)
  typeBuilder.DefineField(&quot;x&quot;, typeof&lt;int&gt;, FieldAttributes.Public)
  let constrBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, [|typeof&lt;int&gt;|])
  let methodBuilder = typeBuilder.DefineMethod(&quot;fun&quot;, MethodAttributes.Public)
  let delegRetType =  System.Linq.Expressions.Expression.GetDelegateType([|typeof&lt;obj&gt; ; typeof&lt;int&gt;|]).GetConstructor([|typeof&lt;obj&gt; ; typeof&lt;nativeint&gt;|]).DeclaringType
  methodBuilder.SetReturnType(delegRetType) 
  methodBuilder.SetParameters([||])
  //buildConstrBody - nothing special
  //buildMethodBody - generating the instructions using the methodBuilder .GetILGenerator() - triggering the error described above
  typeBuilder.CreateType()
  </code></pre>
  <p>Note that <code>delegRetType</code> is actually Func&lt;Object, Integer&gt; instead of Func&lt;A, Integer&gt;. If I try to change it to the latter, the same error is triggered...</p>