Exemple #1
0
        private void ProcessCallbackBinding()
        {
            // Trying to roughly follow http://msdn.microsoft.com/en-us/library/ms228974(v=vs.110).aspx
            // "Event-based async pattern"

            if (InputObject == null)
            {
                throw new PSArgumentNullException("InputObject", "InputObject may not be null.");
            }

            bool isStatic = false;
            Type targetType;

            if ((_baseObject as Type) != null)
            {
                targetType = (Type)_baseObject;
                isStatic   = true;
                this.WriteVerbose("InputObject is a Type: " + targetType.Name);
            }
            else
            {
                targetType = _baseObject.GetType();
                this.WriteVerbose("InputObject is an instance of " + targetType.Name);
            }

            // Func<T1, Tn..., AsyncCallback, object, IAsyncResult> begin,
            // Func<IAsyncResult, dynamic> end)
            // begin/end?
            if (MethodName.StartsWith("Begin",
                                      StringComparison.OrdinalIgnoreCase))
            {
                WriteVerbose("Method is AsyncCallback Begin/End pairing style.");

                string     verb          = MethodName.Substring(5);
                string     endMethodName = "End" + verb;
                MethodInfo endMethod     = targetType.GetMethod(endMethodName, new[] { typeof(IAsyncResult) });

                if (endMethod == null)
                {
                    throw new PSArgumentException(String.Format(
                                                      "No matching '{0}(IAsyncResult)' method found for APM call '{1}'.",
                                                      endMethodName, MethodName));

                    // TODO: throw proper terminating error
                    //this.ThrowTerminatingError(new ErrorRecord());
                }
                //BindBeginEndStyleMethod(targetType, isStatic);
            }
            else if (MethodName.EndsWith("Async", StringComparison.OrdinalIgnoreCase))
            {
                // determine if EAP or TAP mode call
                string verb = MethodName.Substring(0, MethodName.Length - 5); // e.g. "[Read]Async"

                this.WriteWarning("*Async method handling not implemented, yet.");
            }
            // winrt / task?

            //
        }