转换 C# 代码

发布于 2024-08-06 12:33:37 字数 394 浏览 4 评论 0原文

我需要在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

巴黎盛开的樱花 2024-08-13 12:33:38

要做的事情是在项目 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.

誰ツ都不明白 2024-08-13 12:33:38

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文