title: "Operation could destablize the runtime" and DynamicMethod with value types
body: <p>I'm trying to generalize the following IL (from Reflector):</p>
  
  <pre><code>.method private hidebysig instance void SetValue(valuetype Test.TestFixture/ValueSource&amp; thing, string 'value') cil managed
  {
      .maxstack 8
      L_0000: nop 
      L_0001: ldarg.1 
      L_0002: ldarg.2 
      L_0003: call instance void Test.TestFixture/ValueSource::set_Value(string)
      L_0008: nop 
      L_0009: ret 
  }
  </code></pre>
  
  <p>However, when I try and reproduce this IL with DynamicMethod:</p>
  
  <pre><code>        [Test]
      public void Test_with_DynamicMethod()
      {
          var sourceType = typeof(ValueSource);
          PropertyInfo property = sourceType.GetProperty("Value");
  
          var setter = property.GetSetMethod(true);
          var method = new DynamicMethod("Set" + property.Name, null, new[] { sourceType.MakeByRefType(), typeof(string) }, true);
          var gen = method.GetILGenerator();
  
          gen.Emit(OpCodes.Ldarg_1); // Load input to stack
          gen.Emit(OpCodes.Ldarg_2); // Load value to stack
          gen.Emit(OpCodes.Call, setter); // Call the setter method
          gen.Emit(OpCodes.Ret);
  
          var result = (SetValueDelegate)method.CreateDelegate(typeof(SetValueDelegate));
  
          var source = new ValueSource();
  
          result(ref source, "hello");
  
          source.Value.ShouldEqual("hello");
      }
  
      public delegate void SetValueDelegate(ref ValueSource source, string value);
  </code></pre>
  
  <p>I get an exception of "Operation could destabilize the runtime".  The IL seems identical to me, any ideas?  ValueSource is a value type, which is why I'm doing a ref parameter here.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Here's the ValueSource type:</p>
  
  <pre><code>public struct ValueSource
  {
     public string Value { get; set; }
  }
  </code></pre>