2

您好,我正在尝试使用Reflection.Emit动态生成方法,但我不知道如何解释以下代码段:

C# (我想要生成的)

 public Task<Cell> GetAsync(string key)
{
    IEnumerable<Component<Cell>> async = Core.ComponentEnumerator.Instance.GetAsync(key);
    Command<Cell> command = new Command<Cell>(async);
    return this.ProcessAsync<Cell>(command);
}

释放模式下生成的IL

method public hidebysig newslot virtual final instance class [System.Runtime]System.Threading.Tasks.Task`1<valuetype redius.Cell> GetAsync(string key) cil managed
{
    .maxstack 2
    .locals init (
        [0] class [System.Runtime]System.Collections.Generic.IEnumerable`1<valuetype redius.Component`1<valuetype redius.Cell>> enumerable,
        [1] valuetype redius.Command`1<valuetype redius.Cell> command)
    L_0000: call class redius.Core/ComponentEnumerator redius.Core/ComponentEnumerator::get_Instance()
    L_0005: ldarg.1 
    L_0006: callvirt instance class [System.Runtime]System.Collections.Generic.IEnumerable`1<valuetype redius.Component`1<valuetype redius.Cell>> redius.Core/ComponentEnumerator::GetAsync(string)
    L_000b: stloc.0 
    L_000c: ldloca.s command
    L_000e: ldloc.0 
    L_000f: call instance void redius.Command`1<valuetype redius.Cell>::.ctor(class [System.Runtime]System.Collections.Generic.IEnumerable`1<valuetype redius.Component`1<!0>>)
    L_0014: ldarg.0 
    L_0015: ldloc.1 
    L_0016: call instance class [System.Runtime]System.Threading.Tasks.Task`1<!!0> redius.OpsGenTemplate::ProcessAsync<valuetype redius.Cell>(valuetype redius.Command`1<!!0>)
    L_001b: ret 
}

有人可以向我解释以下几点:

1第一条指令L_0000调用了一个单例对象但没有在任何地方存储它的值。为什么它不使用ldarg.0然后将这个值转发到 Command<Cell>构造函数中?
2然后它调用ldarg.1...当在我们的方法中我们只有该方法的 1 个参数时。

我期待着这样的事情:

1.加载参数0 - ldarg.0
2.stloc它在 async变量
3.调用构造函数Command<Cell>消耗上面的行
4.stloc它在command
5.调用ProcessAsync<Cell>
6.ret

4

0 回答 0