title: IL Emit - redirecting a property
body: <p>I'm trying to create a dynamic wrapper, but can fugire out my mistake.
  Here is what I got:</p>
  
  <p>Simple Data class:</p>
  
  <pre><code>public class Data
  {
      public int MyProperty { get; set; } = 42;
  }
  </code></pre>
  
  <p>A Base class, providing a field for redirection:</p>
  
  <blockquote>
  <pre><code>    public class WrapperBase&lt;T&gt; where T : new()
      {
          public WrapperBase()
          {
              Field = new T();
          }
          public T Field { get; set; }
          public void Foo();
      }
  </code></pre>
  </blockquote>
  
  <p>A class using the redirection base:</p>
  
  <pre><code>public class SomeDynamicClass : WrapperBase&lt;Data&gt; { }
  </code></pre>
  
  <p>and a class using it:</p>
  
  <pre><code>public class User
  {
      public User()
      {
          Content = MyFactory.Create&lt;SomeDynamicClass&gt;();
      }
      public object Content { get; set; }
  }
  </code></pre>
  
  <p>This is what I want the factory to create:</p>
  
  <pre><code>public class DesiredResult : SomeDynamicClass
  {
      public int MyProperty
      {
          get { return Field.MyProperty; }
          set
          {
              Field.MyProperty = value;
              Foo();
          }
      }
  }
  </code></pre>
  
  <p>I checked the IL code and tried to recode it, but there is a mistake which I can not find. Here is what I got so far:</p>
  
  <pre><code>public class Factory
  {
      private const String NAMESPACE = "vmproxy.{0}";
      public object Create&lt;T&gt;() where T : class
      {
          AssemblyName an = new AssemblyName();
          an.Name = Guid.NewGuid().ToString();
          AppDomain ad = AppDomain.CurrentDomain;
          AssemblyBuilder ab = ad.DefineDynamicAssembly(an, AssemblyBuilderAccess.Run);
  
          var _mb = ab.DefineDynamicModule(an.Name);
          var fieldProp = typeof(T).GetProperty("Field");
          var modelType = typeof(T).GetProperty("Field").PropertyType;
          var name = typeof(T).Name;
          var tb = _mb.DefineType(String.Format(NAMESPACE, name), TypeAttributes.Public | TypeAttributes.Class, typeof(T));
  
  
          foreach (var property in modelType.GetProperties())
          {
              PropertyBuilder prop = tb.DefineProperty(property.Name, PropertyAttributes.None, property.PropertyType, null);
              MethodBuilder meth = tb.DefineMethod("get_" + property.Name,
                                                          MethodAttributes.Public |
                                                          MethodAttributes.SpecialName |
                                                          MethodAttributes.Virtual |
                                                          MethodAttributes.HideBySig,
                                                          property.PropertyType, Type.EmptyTypes);
              ILGenerator ilGen = meth.GetILGenerator();
              Label endOfMethod = ilGen.DefineLabel();
              ilGen.Emit(OpCodes.Ldarg_0);
              //ilGen.Emit(OpCodes.Call, fieldProp.GetGetMethod());
              ilGen.EmitCall(OpCodes.Call, fieldProp.GetGetMethod(), null);
              //ilGen.Emit(OpCodes.Callvirt, property.GetGetMethod());
              ilGen.EmitCall(OpCodes.Callvirt, property.GetGetMethod(),null);
              ilGen.Emit(OpCodes.Stloc_0);
              ilGen.Emit(OpCodes.Br_S, endOfMethod);
              ilGen.MarkLabel(endOfMethod);
              ilGen.Emit(OpCodes.Ldloc_0);
              ilGen.Emit(OpCodes.Ret);
              prop.SetGetMethod(meth);
  
              //meth = tb.DefineMethod("set_" + property.Name,
              //                                MethodAttributes.Public |
              //                                MethodAttributes.SpecialName |
              //                                MethodAttributes.Virtual |
              //                                MethodAttributes.HideBySig,
              //                                null, new Type[] { property.PropertyType });
              //ilGen = meth.GetILGenerator();
              //ilGen.Emit(OpCodes.Ldarg_0);
              //TBD...
              break;
          }
  
          return Activator.CreateInstance(tb.CreateType());
      }
  </code></pre>
  
  <p>Seems like I am generating an invalid IL code... Is there a way to check the created IL code?...</p>
  
  <p><em>Edit: the IL Code I am trying to create:</em></p>
  
  <pre><code>    .method public hidebysig specialname instance int32 
              get_IntProp() cil managed
      {
        // Code size       17 (0x11)
        .maxstack  1
        .locals init ([0] int32 V_0)
        IL_0000:  nop
        IL_0001:  ldarg.0
        IL_0002:  call       instance !0 class FactoryWrapper.WrapperBase`1&lt;class FactoryWrapper.Data&gt;::get_Field()
        IL_0007:  callvirt   instance int32 FactoryWrapper.Data::get_IntProp()
        IL_000c:  stloc.0
        IL_000d:  br.s       IL_000f
        IL_000f:  ldloc.0
        IL_0010:  ret
      } // end of method DerivedClass3::get_IntProp
  
  
  
  .method public hidebysig specialname instance void 
          set_IntProp(int32 'value') cil managed
  {
    // Code size       27 (0x1b)
    .maxstack  8
    IL_0000:  nop
    IL_0001:  ldarg.0
    IL_0002:  call       instance !0 class FactoryWrapper.WrapperBase`1&lt;class FactoryWrapper.Data1::get_Field()
    IL_0007:  ldarg.1
    IL_0008:  callvirt   instance void FactoryWrapper.Data::set_IntProp(int32)
    IL_000d:  nop
    IL_000e:  ldarg.0
    IL_0014:  callvirt   instance void class FactoryWrapper.WrapperBase`1&lt;class FactoryWrapper.Data1&gt;::Foo()
    IL_0019:  nop
    IL_001a:  ret
  } // end of method DerivedClass3::set_IntProp
  </code></pre>
  
  <p><em>Edit:</em> Receiving "<strong>Unknown local variablenumber at offset 0x000c</strong>"</p>