转换 C# 代码
我需要在 C# 代码(使用 ilGenerator.Emit)中模拟以下函数
public void AssignAttribute(ref ValueHolder output, Assignment assignment) {
ResultAttribute attribute = null;
if ( (attribute = output.MultipleResults.Find(delegate(ResultAttribute o) {
return o.Name == assignment.Name;
})) != null)
attribute.Value = assignment.Value;
}
有人可以帮助我吗?
I need to simulate in C# code (with ilGenerator.Emit) the following function
public void AssignAttribute(ref ValueHolder output, Assignment assignment) {
ResultAttribute attribute = null;
if ( (attribute = output.MultipleResults.Find(delegate(ResultAttribute o) {
return o.Name == assignment.Name;
})) != null)
attribute.Value = assignment.Value;
}
Can anybody help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要做的事情是在项目 C# 中编译该方法,然后使用 反射器。您可以使用 Emit 轻松复制该 IL,并进行所需的任何动态更改。
The thing to do is to compile the method in a project C#, then have a look at the IL in the assembly it generates using Reflector. You can easily replicate that IL using Emit and make whatever dynamic changes you need.
c# 为您创建一个闭包(如果您不熟悉,请参阅维基百科),因为在匿名方法主体中您引用了赋值变量(在您的情况下是参数,但这并不重要)。
您需要为匿名委托创建类持有者(至少 C# 编译器会这样做),
然后您需要在委托关闭后在此类中创建字段(我不是母语英语,所以这里可能拼写错误)
赋值赋值参数
然后在AssignAttribute的主体中你应该发出类实例化
IL_0000: newobj 实例 void V24.Generate.Worker/'<>c__DisplayClass1'::.ctor()
以及字段赋值
IL_0008: stfld class [nviss]NViss.Assignment V24.Generate.Worker/'<>c__DisplayClass1'::assignment
请注意,由于归档初始化已在任何地方完成,对局部变量的访问被替换为对字段的访问,
再次抱歉我的英语
c# create a closure (see wikipedia if you not familiar with) for you since in the anonymous method body you reference to the assignment variable (which is parameter in your case but this doesn't matter).
You need to create class holder for anonymous delegate (at least c# compiler does this)
then you need to create field in this class since your delegate closeover (i'm not native english so here maybe misspelling )
Assignment assignment parameter
Then in the body of AssignAttribute you should emit class instaniation
IL_0000: newobj instance void V24.Generated.Worker/'<>c__DisplayClass1'::.ctor()
as well as feild assignment
IL_0008: stfld class [nviss]NViss.Assignment V24.Generated.Worker/'<>c__DisplayClass1'::assignment
note that since filed initialization finished anywhere access to local variable was replaced with access to field
once again sorry for my English