Imagine we have a mutable struct (yes, don\'t start):
public struct MutableStruct
{
public int Foo { get; set; }
public override string
Well, that was fun.
Using
Actually, it's mostly Unbox (see history for version that works with Ldflda and Stind_*).Ldflda and Stind_* seems to work.
Here's what I hacked together in LinqPad to prove it out.
public struct MutableStruct
{
public int Foo { get; set; }
public override string ToString()
{
return Foo.ToString();
}
}
void Main()
{
var foo = typeof(MutableStruct).GetProperty("Foo");
var setFoo = foo.SetMethod;
var dynMtd = new DynamicMethod("Evil", typeof(void), new [] { typeof(object), typeof(int) });
var il = dynMtd.GetILGenerator();
il.Emit(OpCodes.Ldarg_0); // object
il.Emit(OpCodes.Unbox, typeof(MutableStruct)); // MutableStruct&
il.Emit(OpCodes.Ldarg_1); // MutableStruct& int
il.Emit(OpCodes.Call, setFoo); // --empty--
il.Emit(OpCodes.Ret); // --empty--
var del = (Action