发出 属性 以设置字典值

Emit property to set a dictionary value

我尝试在动态程序集中为 属性 发出一个设置字段方法。

需要的 C# 代码是:

private readonly Dictionary<string, object> propertyBag = new Dictionary<string, object>();

public string PropertyName
{
    set { propertyBag["PropertyName"] = value; }
}

PropertyName 及其类型必须是动态的

我发出 setter 的代码是:

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);

当 运行 设置 属性 的代码时,我有一个

AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

一个想法?

set_item的参数顺序好像不对

看看这个反编译的例子,value (arg1) 应该是第二个参数,而不是第一个:

IL_0000: ldarg.0
IL_0001: ldfld class [System.Private.CoreLib]System.Collections.Generic.Dictionary`2<string, object> C::propertyBag
IL_0006: ldstr "PropertyName"
IL_000b: ldarg.1
IL_000c: callvirt instance void class [System.Private.CoreLib]System.Collections.Generic.Dictionary`2<string, object>::set_Item(!0, !1)
IL_0011: ret

摘自此处: sharplab.io