I try to emit a set field method for a property in a dynamic assembly.
The C# code wanted is :
private readonly Dictionary<string, object> propertyBag = new Dictionary<string, object>();
public string PropertyName
{
set { propertyBag["PropertyName"] = value; }
}
PropertyName and its type must be dynamic
My code to emit the setter is:
PropertyBuilder propertyBuilder = typeBuilder.DefineProperty(propertyName, PropertyAttributes.None, propertyType, Type.EmptyTypes);
FieldBuilder fieldBuilder = typeBuilder.DefineField("propertyBag", typeof(Dictionary<string, object>), FieldAttributes.Private);
MethodBuilder setMethodBuilder = typeBuilder.DefineMethod($"set_{propertyName}", MethodAttributes.Public | MethodAttributes.HideBySig, null, new[] { propertyType });
ILGenerator setIL = setMethodBuilder.GetILGenerator();
setIL.Emit(OpCodes.Ldarg_0);
setIL.Emit(OpCodes.Ldarg_1);
setIL.Emit(OpCodes.Ldfld, fieldBuilder);
setIL.Emit(OpCodes.Ldstr, propertyName);
setIL.Emit(OpCodes.Callvirt, fieldBuilder.FieldType.GetMethod("set_Item", new Type[] { typeof(string), typeof(object) }));
setIL.Emit(OpCodes.Ret);
propertyBuilder.SetSetMethod(setMethodBuilder);
When running the code which set the property, i have an
AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
An idea?