发出操作码将字段设置为一个值
我正在尝试动态创建代理,因此使用 Emit 来实现。 因此,当我设置我的字段时,我还需要将 isDirty 字段布尔值设置为 true。
我怎样才能做到这一点?
Property Customer
{
set
{
this.customerName = value;
this.isDirty = true;
}
}
发出代码:
FieldBuilder isDirtyField = myTypeBuilder.DefineField("isDirty", typeof(bool), FieldAttributes.Private);
// Define the "set" accessor method for CustomerName.
MethodBuilder custNameSetPropMthdBldr =
myTypeBuilder.DefineMethod("set_CustomerName",
getSetAttr,
null,
new Type[] { typeof(string) });
ILGenerator custNameSetIL = custNameSetPropMthdBldr.GetILGenerator();
custNameSetIL.Emit(OpCodes.Ldarg_0);
custNameSetIL.Emit(OpCodes.Ldarg_1);
custNameSetIL.Emit(OpCodes.Stfld, customerNameBldr);
{
custNameSetIL.EmitWriteLine("Start isDirty");
... do stuf here
custNameSetIL.EmitWriteLine("End isDirty");
}
custNameSetIL.Emit(OpCodes.Ret);
只要我不尝试执行 isDirty 字段,此代码就可以工作,在这个问题上度过了周末,我试图在这个论坛中获得一些帮助。谢谢
//丹尼斯
I am trying dynamic create a proxy, so im pleying with Emit.
So when I set my field with emit I also need to set a isDirty field boolan to true.
How can I do that ?
Property Customer
{
set
{
this.customerName = value;
this.isDirty = true;
}
}
emit code:
FieldBuilder isDirtyField = myTypeBuilder.DefineField("isDirty", typeof(bool), FieldAttributes.Private);
// Define the "set" accessor method for CustomerName.
MethodBuilder custNameSetPropMthdBldr =
myTypeBuilder.DefineMethod("set_CustomerName",
getSetAttr,
null,
new Type[] { typeof(string) });
ILGenerator custNameSetIL = custNameSetPropMthdBldr.GetILGenerator();
custNameSetIL.Emit(OpCodes.Ldarg_0);
custNameSetIL.Emit(OpCodes.Ldarg_1);
custNameSetIL.Emit(OpCodes.Stfld, customerNameBldr);
{
custNameSetIL.EmitWriteLine("Start isDirty");
... do stuf here
custNameSetIL.EmitWriteLine("End isDirty");
}
custNameSetIL.Emit(OpCodes.Ret);
This code is working, as long im not trying to do the isDirty field, having spent the weekend on this, im trying to get some help in this forum. thx
// dennis
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信你想要的 IL 指令的顺序是
I believe that the sequence of IL instructions you want will be