title: Reflection.Emit - IL - call method on object
body: <p>I'm working on creating a dynamic method at runtime to copy an object. Lets assume:</p>
  
  <pre><code>class Source
  {
     public List&lt;int&gt; L1 {get;set;}
  }
  
  class Dest
  {
     public List&lt;int&gt; L1 {get;set;}
  }
  </code></pre>
  
  <p>Now, this situation works correctly. I get Source.L1 and I set Dest.L1. I do so with the following IL:</p>
  
  <pre><code>        generator.Emit(OpCodes.Newobj, constructor);
          generator.Emit(OpCodes.Dup);
          generator.Emit(OpCodes.Ldarg_0);
          generator.Emit(OpCodes.Callvirt, miGetter);
          generator.Emit(OpCodes.Callvirt, miSetter);
          generator.Emit(OpCodes.Ret);
  </code></pre>
  
  <p>All this is working fine... now comes the tricky part. Lets change Dest to:</p>
  
  <pre><code>class Dest
  {
     private List&lt;int&gt; _l1 = new List&lt;int&gt;();
     public List&lt;int&gt; L1 {get { return _l1; } }
  }
  </code></pre>
  
  <p>Now what I want to do in this case is call Dest.L1.Clear() and then Dest.L1.AddRange(...).</p>
  
  <p>I can't even get the .Clear to work.</p>
  
  <p>I still will have:</p>
  
  <pre><code>        generator.Emit(OpCodes.Newobj, constructor);
  
          // this block is repeated 5 times for various properties
  
          generator.Emit(OpCodes.Dup);
          generator.Emit(OpCodes.Ldarg_0);
          generator.Emit(OpCodes.Callvirt, miGetter);
          generator.Emit(OpCodes.Callvirt, miSetter);
  
          // List property will be copied here
          // miGetter = Dest.L1.Get
          // TODO
          // end list property
  
          generator.Emit(OpCodes.Ret);
  </code></pre>
  
  <p>How do I need to set up the IL in the TODO block? I tried doing dup/loadArg0/call miGetter/call miClear, but that gave me an invalid program.</p>