实现的功能同 [【05-使用C#6.0】08-自定义awaitable类型](http://liujiajia.me/2017/8/8/csharp-multi-threading-05-csharp6-08-customize-awaitable) 是一样的,只是这里是使用动态类型实现的。 *注意:本例需要导入 `ImpromptuInterface` 包* ```csharp using ImpromptuInterface; using System; using System.Dynamic; using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; namespace Recipe5_9 { class Program { /// <summary> /// 对动态类型使用await /// </summary> /// <param name="args"></param> static void Main(string[] args) { Task t = AsynchronousProcessing(); t.Wait(); Console.ReadLine(); } static async Task AsynchronousProcessing() { // 通过参数指定IsCompleted为true,只需同步调用GetResult方法 string result = await GetDynamicAwaitableObject(true); Console.WriteLine(result); // 通过参数指定IsCompleted为false,则会先执行OnCompleted方法 result = await GetDynamicAwaitableObject(false); Console.WriteLine(result); } static dynamic GetDynamicAwaitableObject(bool completeSynchronously) { // ExpandoObject类型可在运行时动态添加和删除其成员的对象 dynamic result = new ExpandoObject(); // 类型t dynamic awaiter = new ExpandoObject(); // 类型A awaiter.Message = "Completed synchronously"; awaiter.IsCompleted = completeSynchronously; awaiter.GetResult = (Func<string>)(() => awaiter.Message); awaiter.OnCompleted = (Action<Action>)(callback => ThreadPool.QueueUserWorkItem(state => { Thread.Sleep(TimeSpan.FromSeconds(3)); awaiter.Message = GetInfo(); callback?.Invoke(); })); // 使用Impromptu.ActLike方法动态的创建代理对象,该对象将实现任何需要的接口 IAwaiter<string> proxy = Impromptu.ActLike(awaiter); // t有一个名为GetAwaiter的可访问的实例或扩展方法 result.GetAwaiter = (Func<dynamic>)(() => proxy); return result; } static string GetInfo() { return $"Task is running on a thread id {Thread.CurrentThread.ManagedThreadId}. Is thread pool thread: {Thread.CurrentThread.IsThreadPoolThread}"; } } public interface IAwaiter<T> : INotifyCompletion { bool IsCompleted { get; } T GetResult(); } } ``` 运行结果: ``` Completed synchronously Task is running on a thread id 3. Is thread pool thread: True ``` Loading... 版权声明:本文为博主「佳佳」的原创文章,遵循 CC 4.0 BY-NC-SA 版权协议,转载请附上原文出处链接及本声明。 原文链接:https://www.liujiajia.me/2017/8/15/csharp-multi-threading-05-csharp6-09-use-await-with-dynamic 提交