为了更懒,我只是想写更少的代码但做同样的事情,所以我做了一个测试。只想使用一些属性并使用 mono.cecil 用一些 il 代码注入 .dll 文件。以下是详细信息:编码时:
[FillProperty("Counter")]
public int Counter
{
get;
set;
}
注入后,有新代码生成(通过 ILSpy)(C# 代码):
public int Counter
{
[CompilerGenerated]
get
{
return this.<Counter>k__BackingField;
}
[CompilerGenerated]
set
{
if (this.<Counter>k__BackingField != value)
{
this.<Counter>k__BackingField = value;
this.<Counter>k__BackingField.TriggerEvent("Counter");
}
}
}
定义代码:
`private int <Counter>k__BackingField;`
看不出来,但是IL代码没问题:
IL_0000: ldarg.0
IL_0001: ldfld int32 AssemblyTest::'<Counter>k__BackingField'
IL_0006: ldarg.1
IL_0007: beq IL_0023
IL_000c: ldarg.0
IL_000d: ldarg.1
IL_000e: stfld int32 AssemblyTest::'<Counter>k__BackingField'
IL_0013: ldarg.0
IL_0014: ldfld int32 AssemblyTest::'<Counter>k__BackingField'
IL_0019: ldstr "Counter"
IL_001e: callvirt void ['Assembly-CSharp']ExMethod::TriggerEvent<int32>(!!0, string)
IL_0023: ret
定义代码也很好:
.field private int32 '<Counter>k__BackingField'
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
01 00 00 00
)
但是当我运行注入的代码时,它会抛出一个异常:“NullReferenceException:对象引用未设置为对象 AssemblyTest.set_Counter(Int32 值)的实例”。那么有没有人可以给我一些提示。