我的问题与这篇文章有关,Intercept the call to an async method using DynamicProxy
我想实现拦截器,它与返回Task
或Task<T>
结果的异步方法一起工作。
我使用next代码返回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函数,不能直接传递。
https://stackoverflow.com/questions/28099669
复制相似问题