简体   繁体   English

转换C#代码

[英]Convert C# code

I need to simulate in C# code (with ilGenerator.Emit) the following function 我需要在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;
    }

Can anybody help me? 有谁能够帮助我?

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 . 要做的事情是在项目C#中编译该方法,然后查看它使用Reflector生成的程序集中的IL。 You can easily replicate that IL using Emit and make whatever dynamic changes you need. 您可以使用Emit轻松复制该IL,并进行所需的任何动态更改。

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). C#为您创建一个闭包(如果您不熟悉,请参见Wikipedia),因为在匿名方法主体中您引用了赋值变量(在您的情况下为参数,但这无关紧要)。

You need to create class holder for anonymous delegate (at least c# compiler does this) 您需要为匿名委托创建类持有人(至少由c#编译器执行此操作)

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() 然后,在AssignAttribute主体中,您应该发出类实例化IL_0000:newobj实例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 以及虚假分配IL_0008:stfld类[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 再次抱歉我的英语

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM