c# 在Emit代码中如何await一个异步方法
0. 前言
首先立马解释一波为啥会有这样一篇伪标题的demo随笔呢?
不是本人有知识误区,或者要误人子弟
因为大家都知道emit写出来的都是同步方法,不可能await,至少现在这么多年来没有提供对应的功能
这是之前某天在微信群看见讨论怎么emit一个异步方法并包装异步结构,简单几句文字也未能清晰的表达
所以趁着元旦节放假有点时间,
简单列举三种我知道方式去达到这样的效果
三种方法都是绕过emit直接书写emit代码,而是将对应逻辑转到其他方法中,最后emit调用方法达到效果
demo 说明
原始方法是个延迟2秒之后返回55的方法:
public static async task<int> getv() { await task.delay(2000); return 55; }
现在我们需要把 55 的结果加 6 ,让最终的结果变为 61
我们的测试方法是这样,会输出一些简单的时间,帮助我们了解执行顺序和异步情况
private static async task test(methodinfo method, methodinfo awaitmehtod) { var caller = createcaller(method, awaitmehtod); console.writeline($"start {awaitmehtod.name} at: {datetime.now}."); var task = caller(); console.writeline($"call done at: {datetime.now}."); var number = await task; console.writeline($"hello {number} at: {datetime.now}."); console.writeline($"end at: {datetime.now}."); console.writeline(); }
1. continuewith
public static func<task<int>> createcaller(methodinfo method, methodinfo awaitmehtod) { var m = new dynamicmethod(guid.newguid().tostring("n"), typeof(task<int>), type.emptytypes); var il = m.getilgenerator(); il.emit(opcodes.call, method); il.emit(opcodes.call, typeof(program).getmethod(nameof(program.addsixusecontinuewith))); // 这里是差异点 il.emit(opcodes.ret); return m.createdelegate(typeof(func<task<int>>)) as func<task<int>>; } public static task<int> addsixusecontinuewith(task<int> task) { return task.continuewith(i => { console.writeline($"addsixusecontinuewith is: {datetime.now}."); return i.result + 6; }); }
测试结果:
start addsixusecontinuewith at: 2021/1/2 13:34:55. call done at: 2021/1/2 13:34:55. addsixusecontinuewith is: 2021/1/2 13:34:57. hello 61 at: 2021/1/2 13:34:57. end at: 2021/1/2 13:34:57.
优点
还是真正的异步
缺点
成本比较大,毕竟这样没有了状态机等等优化,(成本在 ns 级别哦,不是大家想的 ms哦)
2. getawaiter().getresult()
public static func<task<int>> createcaller(methodinfo method, methodinfo awaitmehtod) { var m = new dynamicmethod(guid.newguid().tostring("n"), typeof(task<int>), type.emptytypes); var il = m.getilgenerator(); il.emit(opcodes.call, method); il.emit(opcodes.call, typeof(program).getmethod(nameof(program.addsixuseawaiter))); // 这里是差异点 il.emit(opcodes.ret); return m.createdelegate(typeof(func<task<int>>)) as func<task<int>>; } public static task<int> addsixuseawaiter(task<int> task) { var r = task.configureawait(false).getawaiter().getresult() + 6; console.writeline($"addsixuseawaiter is: {datetime.now}."); return task.fromresult(r); }
测试结果:
start addsixuseawaiter at: 2021/1/2 13:34:57. addsixuseawaiter is: 2021/1/2 13:34:59. call done at: 2021/1/2 13:34:59. hello 61 at: 2021/1/2 13:34:59. end at: 2021/1/2 13:34:59.
优点
执行时间上消耗很小
缺点
当然这样 异步都变成了同步,所以可能会在某些情况下我们操作不当的代码从而导致失去异步方法的优势
3. async/await
public static func<task<int>> createcaller(methodinfo method, methodinfo awaitmehtod) { var m = new dynamicmethod(guid.newguid().tostring("n"), typeof(task<int>), type.emptytypes); var il = m.getilgenerator(); il.emit(opcodes.call, method); il.emit(opcodes.call, typeof(program).getmethod(nameof(program.addsixuseasyncawait))); // 这里是差异点 il.emit(opcodes.ret); return m.createdelegate(typeof(func<task<int>>)) as func<task<int>>; } public static async task<int> addsixuseasyncawait(task<int> task) { var r = await task; console.writeline($"addsixuseasyncawait is: {datetime.now}."); return r + 6; }
测试结果:
start addsixuseasyncawait at: 2021/1/2 13:34:59. call done at: 2021/1/2 13:34:59. addsixuseasyncawait is: 2021/1/2 13:35:01. hello 61 at: 2021/1/2 13:35:01. end at: 2021/1/2 13:35:01.
优点
async / await 本身的优势都没有损失
缺点
原本想在 emit 中 对result的处理逻辑 必须迁移到 async / await 方法中,emit代码必须好好设计
完整demo放在
https://github.com/fs7744/grocery/blob/main/csharp/emit_await/emitawaitdemo/program.cs
分享不易,如果能给予一点动力,不胜感激:关注一下本人的开源项目: norns.urd
以上就是c# 在emit代码中如何await一个异步方法的详细内容,更多关于c# emit代码await一个异步方法的资料请关注其它相关文章!
上一篇: 华为机试----字符串最后一个单词的长度