Dynamic Property setter c#
<p>Hi guys I am trying to generate a delegate at runtime using Dynamic Methods and the ILGenerator to invoke the Setter of a property on an object.....</p>

<pre><code>private static GenericIntSetter CreateIntSetMethod(PropertyInfo propertyInfo)
    {
        MethodInfo setMethod = propertyInfo.GetSetMethod();

        if (setMethod == null)
            return null;

        Type[] arguments = new Type[2];

        arguments[0] = typeof(Person);
        arguments[1] = typeof(int);

        DynamicMethod setter = new DynamicMethod(
             String.Concat("_Set", propertyInfo.Name, "_"),
             typeof(void), arguments, propertyInfo.DeclaringType);
        ILGenerator generator = setter.GetILGenerator();
        generator.Emit(OpCodes.Ldarg_0);
        generator.Emit(OpCodes.Castclass, propertyInfo.DeclaringType);
        generator.Emit(OpCodes.Ldarg_1);

        if (propertyInfo.PropertyType.IsClass)
        {
            generator.Emit(OpCodes.Castclass, propertyInfo.PropertyType);
        }
        else
        {
            generator.Emit(OpCodes.Unbox_Any, propertyInfo.PropertyType);
        }

        generator.EmitCall(OpCodes.Callvirt, setMethod, null);
        generator.Emit(OpCodes.Ret);

        return (GenericIntSetter)setter.CreateDelegate(typeof(GenericIntSetter));
    }
</code></pre>

<p>When I call this method and I invoke the delete I get a corrupt memory error, any ideas?</p>
