Ejemplo n.º 1
0
        //
        // Return container with awaiter definition. It never returns null
        // but all container member can be null for easier error reporting
        //
        public AwaiterDefinition GetAwaiter(TypeSpec type)
        {
            AwaiterDefinition awaiter;

            if (awaiters.TryGetValue(type, out awaiter))
            {
                return(awaiter);
            }

            awaiter = new AwaiterDefinition();

            //
            // Predefined: bool IsCompleted { get; }
            //
            awaiter.IsCompleted = MemberCache.FindMember(type, MemberFilter.Property("IsCompleted", Compiler.BuiltinTypes.Bool),
                                                         BindingRestriction.InstanceOnly) as PropertySpec;

            //
            // Predefined: GetResult ()
            //
            // The method return type is also result type of await expression
            //
            awaiter.GetResult = MemberCache.FindMember(type, MemberFilter.Method("GetResult", 0,
                                                                                 ParametersCompiled.EmptyReadOnlyParameters, null),
                                                       BindingRestriction.InstanceOnly) as MethodSpec;

            //
            // Predefined: INotifyCompletion.OnCompleted (System.Action)
            //
            var nc = PredefinedTypes.INotifyCompletion;

            awaiter.INotifyCompletion = !nc.Define() || type.ImplementsInterface(nc.TypeSpec, false);

            awaiters.Add(type, awaiter);
            return(awaiter);
        }