【发布时间】:2019-09-19 20:25:06
【问题描述】:
我的问题与这篇帖子有关Intercept the call to an async method using DynamicProxy
我想实现与返回 Task 或 Task<T> 结果的异步方法一起使用的拦截器。
我使用下一个代码返回 ContinueWith 结果(以便调用者方法在拦截器完成工作时等待)
var task = invocation.ReturnValue as Task;
invocation.ReturnValue = task.ContinueWith(c =>
{ code that should execute after method finish });
以上代码适用于Task 结果,但如果Task<T> 结果ContinueWith 将返回类型从Task<T> 更改为Task。
我需要调用返回Task<T> 的重载方法ContinueWith,但为此我需要将invocation.ReturnValue 转换为Task<T>
我没有找到以任何方式动态投射它的方法。 有人知道怎么做吗?
我也试过通过反射调用这个方法,但是参数是labmda函数,不能直接传递。
【问题讨论】:
-
查看msdn.microsoft.com/en-us/magazine/dn574805.aspx - 文章适用于 Unity 拦截器,但“包装任务”代码不依赖于您的 DI 容器。
标签: c# generics async-await castle-dynamicproxy