如何使用 mono cecil 返回 Task.FromResult<bool>(true)?

问题描述

我需要使用mono cecil来实现下面的逻辑。

public Task<bool> MyTask()
{
    return Task.FromResult<bool>(true);
}

我只知道如何调用非泛型方法,因为在cecil项目中有相应的演示代码。我不知道如何调用泛型方法。谁能告诉我该怎么做?相应的 IL 代码应如下所示:

ldc.i4.1 调用类 [mscorlib]System.Threading.Tasks.Task`1 [mscorlib]System.Threading.Tasks.Task::FromResult(!!0) 返回

这就是我现在所拥有的:

void Main()
{
    AssemblyDefinition asm = AssemblyDefinition.ReadAssembly(@"F:\files.fm\Code\asm_wrangler\asm_wrangler\bin\Debug\a.exe");

    MethodInfo writeLineMethod = typeof(Debug).GetMethod("WriteLine",new Type[] { typeof(string) });
    MethodReference writeLine = asm.MainModule.Import(writeLineMethod);

    foreach (ModuleDefinition module in asm.Modules)
    {
        foreach (TypeDefinition type in module.Types)
        {
            foreach (var method in type.Methods)
            { 
                if (method.Name == "MyTask")
                {
                    var ins = method.Body.Instructions;
                    ins.Clear();
                    ILProcessor processor = method.Body.GetILProcessor();
                    
                    ins.Add(processor.Create(OpCodes.Ldc_I4_1));
                    
                    // ===> How to invoke Task.FromResult<bool>(true) here?
                    
                    ins.Add(processor.Create(OpCodes.Ret));
                }
            }
        }
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)