﻿<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>Ayende @ Rahien</title><link>http://ayende.com</link><description>Ayende @ Rahien</description><copyright>Copyright (C) Ayende Rahien  2004 - 2021 (c) 2026</copyright><ttl>60</ttl><item><title>Fabian Schmied commented on System.Reflection.Emit fun: Find the differences</title><description>With SRE, it's often easy to confuse generic parameter references: when you define a generic parameter, you must always refer to that parameter using the returned GenericTypeParameterBuilder, even if you think you have found some other way to get to the same parameter. That has bitten us a few times within DynamicProxy already.
  
  
I tried, and I think I managed to get an implementation of your problem that works:
  
  
using System;
  
using System.Reflection.Emit;
  
using System.Reflection;
  
  
namespace ConsoleApplication46
  
{
  
  class Program
  
  {
  
    static void Main (string[] args)
  
    {
  
      var name = new AssemblyName ("MyAssembly");
  
      AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly (name, AssemblyBuilderAccess.RunAndSave);
  
      ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule ("MyAssembly.dll");
  
      TypeBuilder typeBuilder = moduleBuilder.DefineType ("Generated", TypeAttributes.Public);
  
      GenericTypeParameterBuilder[] genericParameterBuilders = typeBuilder.DefineGenericParameters ("T");
  
      FieldBuilder argumentsFieldBuilder = typeBuilder.DefineField ("Arguments", typeof (object[]), FieldAttributes.Public);
  
      MethodBuilder fooMethodBuilder = typeBuilder.DefineMethod ("Foo", MethodAttributes.Public);
  
      fooMethodBuilder.SetParameters (genericParameterBuilders[0].MakeByRefType ());
  
      fooMethodBuilder.DefineParameter (1, ParameterAttributes.Out, "blah");
  
      ILGenerator ilGenerator = fooMethodBuilder.GetILGenerator ();
  
  
      ilGenerator.Emit (OpCodes.Ldarg_1);
  
      ilGenerator.Emit (OpCodes.Ldarg_0);
  
      ilGenerator.Emit (OpCodes.Ldfld, argumentsFieldBuilder);
  
      ilGenerator.Emit (OpCodes.Ldc_I4_0);
  
      ilGenerator.Emit (OpCodes.Ldelem_Ref);
  
      ilGenerator.Emit (OpCodes.Unbox_Any, genericParameterBuilders[0]);
  
      ilGenerator.Emit (OpCodes.Stobj, genericParameterBuilders[0]);
  
      ilGenerator.Emit (OpCodes.Ret);
  
  
      typeBuilder.CreateType ();
  
  
      assemblyBuilder.Save ("MyAssembly.dll");
  
    }
  
  
    class Reference&lt;T&gt;
  
    {
  
      public object[] Arguments;
  
  
      public void Foo (out T blah)
  
      {
  
        blah = (T) Arguments[0];
  
      }
  
    }
  
  }
  
}
  
  
Although, judging from the "!!T" in your disassembly, did you mean Foo&lt;T&gt;? Then it would be as follows:
  
  
var name = new AssemblyName ("MyAssembly");
  
      AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly (name, AssemblyBuilderAccess.RunAndSave);
  
      ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule ("MyAssembly.dll");
  
      TypeBuilder typeBuilder = moduleBuilder.DefineType ("Generated", TypeAttributes.Public);
  
      FieldBuilder argumentsFieldBuilder = typeBuilder.DefineField ("Arguments", typeof (object[]), FieldAttributes.Public);
  
      MethodBuilder fooMethodBuilder = typeBuilder.DefineMethod ("Foo", MethodAttributes.Public);
  
      GenericTypeParameterBuilder[] genericParameterBuilders = fooMethodBuilder.DefineGenericParameters ("T");
  
      fooMethodBuilder.SetParameters (genericParameterBuilders[0].MakeByRefType ());
  
      fooMethodBuilder.DefineParameter (1, ParameterAttributes.Out, "blah");
  
      ILGenerator ilGenerator = fooMethodBuilder.GetILGenerator ();
  
  
      ilGenerator.Emit (OpCodes.Ldarg_1);
  
      ilGenerator.Emit (OpCodes.Ldarg_0);
  
      ilGenerator.Emit (OpCodes.Ldfld, argumentsFieldBuilder);
  
      ilGenerator.Emit (OpCodes.Ldc_I4_0);
  
      ilGenerator.Emit (OpCodes.Ldelem_Ref);
  
      ilGenerator.Emit (OpCodes.Unbox_Any, genericParameterBuilders[0]);
  
      ilGenerator.Emit (OpCodes.Stobj, genericParameterBuilders[0]);
  
      ilGenerator.Emit (OpCodes.Ret);
  
  
      typeBuilder.CreateType ();
  
  
      assemblyBuilder.Save ("MyAssembly.dll");
  
</description><link>http://ayende.com/3516/system-reflection-emit-fun-find-the-differences#comment9</link><guid>http://ayende.com/3516/system-reflection-emit-fun-find-the-differences#comment9</guid><pubDate>Wed, 13 Aug 2008 16:06:46 GMT</pubDate></item><item><title>runefs commented on System.Reflection.Emit fun: Find the differences</title><description>aside from you using the 8 byte nonconstant version of ldc.i4 instead of the constant, asuming that local2 has the same type as local1 there's no difference in the two snippets. since ldc.i4 0 is not invalid that should not be the problem. If run peverify on the code what's the error message?
</description><link>http://ayende.com/3516/system-reflection-emit-fun-find-the-differences#comment8</link><guid>http://ayende.com/3516/system-reflection-emit-fun-find-the-differences#comment8</guid><pubDate>Wed, 13 Aug 2008 08:51:52 GMT</pubDate></item><item><title>liviu commented on System.Reflection.Emit fun: Find the differences</title><description>Maybe,
  
  
the error is in the code you don't show. In the way you defined your local variables, etc...
  
I played with reflection emit a lot lately (not so much with generics) and i know that finding the problems is really hard, the dreadfull Invalid program Exception is so useless...
</description><link>http://ayende.com/3516/system-reflection-emit-fun-find-the-differences#comment7</link><guid>http://ayende.com/3516/system-reflection-emit-fun-find-the-differences#comment7</guid><pubDate>Wed, 13 Aug 2008 08:32:07 GMT</pubDate></item><item><title>Ayende Rahien commented on System.Reflection.Emit fun: Find the differences</title><description>This is the same think.
  
The second is a way to specify this with one less byte/int
</description><link>http://ayende.com/3516/system-reflection-emit-fun-find-the-differences#comment6</link><guid>http://ayende.com/3516/system-reflection-emit-fun-find-the-differences#comment6</guid><pubDate>Wed, 13 Aug 2008 07:39:33 GMT</pubDate></item><item><title>Dennis commented on System.Reflection.Emit fun: Find the differences</title><description>L_008c: ldc.i4 0 &lt;-- 4&lt;space&gt;0
  
L_0010: ldc.i4.0 &lt;-- 4&lt;dot&gt;0
</description><link>http://ayende.com/3516/system-reflection-emit-fun-find-the-differences#comment5</link><guid>http://ayende.com/3516/system-reflection-emit-fun-find-the-differences#comment5</guid><pubDate>Wed, 13 Aug 2008 06:28:30 GMT</pubDate></item><item><title>Steve commented on System.Reflection.Emit fun: Find the differences</title><description>L_0010 is a shorthand version of L_008c.
  
  
According to the documentation they should behave identically. 
  
  
Is there some undocumented difference?
</description><link>http://ayende.com/3516/system-reflection-emit-fun-find-the-differences#comment4</link><guid>http://ayende.com/3516/system-reflection-emit-fun-find-the-differences#comment4</guid><pubDate>Wed, 13 Aug 2008 03:38:27 GMT</pubDate></item><item><title>Neal Blomfield commented on System.Reflection.Emit fun: Find the differences</title><description>Assuming the difference between equivalent lines L_008c and L_0010 is a typo (and ignoring that your generated code uses an extra variable), there does not seem to be any difference at all between these 2.  
  
  
Can you create a valid implementation with SRE that mimics the valid example exactly (i.e. no variations in args or variables)?
  
  
Hoping you find the issue and curious as to what has caused it.
</description><link>http://ayende.com/3516/system-reflection-emit-fun-find-the-differences#comment3</link><guid>http://ayende.com/3516/system-reflection-emit-fun-find-the-differences#comment3</guid><pubDate>Wed, 13 Aug 2008 02:49:02 GMT</pubDate></item><item><title>Jeremy Gray commented on System.Reflection.Emit fun: Find the differences</title><description>Err, brain-fart on my part.: when I said "ldloc difference" I meant to say "ldarg versus ldloc difference across examples 1 and 2".
  
  
:)
</description><link>http://ayende.com/3516/system-reflection-emit-fun-find-the-differences#comment2</link><guid>http://ayende.com/3516/system-reflection-emit-fun-find-the-differences#comment2</guid><pubDate>Wed, 13 Aug 2008 01:08:18 GMT</pubDate></item><item><title>Jeremy Gray commented on System.Reflection.Emit fun: Find the differences</title><description>I don't often work down at that low of a level, but the ldloc difference stood out to me. Related to your issue, perhaps? (not that I would be in a position to say how ;)
</description><link>http://ayende.com/3516/system-reflection-emit-fun-find-the-differences#comment1</link><guid>http://ayende.com/3516/system-reflection-emit-fun-find-the-differences#comment1</guid><pubDate>Wed, 13 Aug 2008 01:07:14 GMT</pubDate></item></channel></rss>