Beispiel #1
0
        public override bool GenerateDynamicParameters()
        {
            var thisIsFirstObject = false;

            try {
                if (!IsReentrantLocked)
                {
                    // we're not locked at this point. Let's turn on the lock.
                    IsReentrantLocked = true;
                    thisIsFirstObject = true;

                    try {
                        // do all the work that we need to during the lock
                        // this includes:
                        //      one-time-per-call work
                        //      any access to MyInvocation.MyCommand.*
                        //      modifying parameter validation sets
                        //

                        if (MyInvocation != null && MyInvocation.MyCommand != null && MyInvocation.MyCommand.Parameters != null)
                        {
                            GetType().AddOrSet(MyInvocation.MyCommand.Parameters, "MyInvocation.MyCommand.Parameters");
                        }
#if DEEP_DEBUG
                        else
                        {
                            if (MyInvocation == null)
                            {
                                Console.WriteLine("»»» Attempt to get parameters MyInvocation == NULL");
                            }
                            else
                            {
                                if (MyInvocation.MyCommand == null)
                                {
                                    Console.WriteLine("»»» Attempt to get parameters MyCommand == NULL");
                                }
                                else
                                {
                                    Console.WriteLine("»»» Attempt to get parameters Parameters == NULL");
                                }
                            }
                        }
#endif


                        // the second time, it will generate all the parameters, including the dynamic ones.
                        // (not that we currently need it, but if you do, you gotta do it here!)
                        // var all_parameters = MyInvocation.MyCommand.Parameters;

                        // ask for the unbound arguments.
                        var unbound = UnboundArguments;
                        if (unbound.ContainsKey("ProviderName") || unbound.ContainsKey("Provider"))
                        {
                            var pName = unbound.ContainsKey("ProviderName")?unbound["ProviderName"]:unbound["Provider"];
                            if (pName != null)
                            {
                                if (pName.GetType().IsArray)
                                {
                                    ProviderName = pName as string[] ?? (((object[])pName).Select(p => p.ToString()).ToArray());
                                }
                                else
                                {
                                    ProviderName = new[] { pName.ToString() };
                                }

                                // a user specifies -providerName
                                _isUserSpecifyOneProviderName = (ProviderName.Count() == 1);
                            }
                        }

                        // we've now got a copy of the arguments that aren't bound
                        // and we can potentially narrow the provider selection based
                        // on arguments the user specified.

                        if (null == CachedSelectedProviders || IsFailingEarly || IsCanceled)
                        {
#if DEEP_DEBUG
                            Console.WriteLine("»»» Cancelled before we got finished doing dynamic parameters");
#endif
                            // this happens if there is a serious failure early in the cmdlet
                            // i.e. - if the SelectedProviders comes back empty (due to agressive filtering)

                            // in this case, we just want to provide a catch-all for remaining arguments so that we can make
                            // output the error that we really want to (that the user specified conditions that filtered them all out)

                            DynamicParameterDictionary.Add("RemainingArguments", new RuntimeDefinedParameter("RemainingArguments", typeof(object), new Collection <Attribute> {
                                new ParameterAttribute()
                                {
                                    ValueFromRemainingArguments = true
                                },
                            }));
                        }

                        // at this point, we're actually calling to have the dynamic parameters generated
                        // that are expected to be used.
                        return(ActualGenerateDynamicParameters(unbound));
                    } finally {
                        IsReentrantLocked = false;
                    }
                }

                // otherwise just call the AGDP because we're in a reentrant call.
                // and this might be needed if the original call had some strange need
                // to know what the parameters that it's about to generate would be.
                // Yeah, you heard me.
                return(ActualGenerateDynamicParameters(null));
            } finally
            {
                if (thisIsFirstObject)
                {
                    // clean up our once-per-call data.
                    GetType().Remove <PackageProvider[]>("CachedSelectedProviders");
                    GetType().Remove <Dictionary <string, ParameterMetadata> >("MyInvocation.MyCommand.Parameters");
                    GetType().Remove <DynamicOption[]>("CachedDynamicOptions");
                }
            }
            // return true;
        }