Exemplo n.º 1
0
 private void ValidateNullOrEmptyArgument(CommandParameterInternal parameter, CompiledCommandParameter parameterMetadata, Type argumentType, object parameterValue, bool recurseIntoCollections)
 {
     if ((parameterValue == null) && (argumentType != typeof(bool?)))
     {
         if (!parameterMetadata.AllowsNullArgument)
         {
             bindingTracer.WriteLine("ERROR: Argument cannot be null", new object[0]);
             ParameterBindingValidationException exception = new ParameterBindingValidationException(ErrorCategory.InvalidData, this.InvocationInfo, this.GetErrorExtent(parameter), parameterMetadata.Name, argumentType, (parameterValue == null) ? null : parameterValue.GetType(), "ParameterBinderStrings", "ParameterArgumentValidationErrorNullNotAllowed", new object[0]);
             throw exception;
         }
     }
     else if (argumentType == typeof(string))
     {
         string str = parameterValue as string;
         if ((str.Length == 0) && !parameterMetadata.AllowsEmptyStringArgument)
         {
             bindingTracer.WriteLine("ERROR: Argument cannot be an empty string", new object[0]);
             ParameterBindingValidationException exception2 = new ParameterBindingValidationException(ErrorCategory.InvalidData, this.InvocationInfo, this.GetErrorExtent(parameter), parameterMetadata.Name, parameterMetadata.Type, (parameterValue == null) ? null : parameterValue.GetType(), "ParameterBinderStrings", "ParameterArgumentValidationErrorEmptyStringNotAllowed", new object[0]);
             throw exception2;
         }
     }
     else if (recurseIntoCollections)
     {
         switch (parameterMetadata.CollectionTypeInformation.ParameterCollectionType)
         {
         case ParameterCollectionType.IList:
         case ParameterCollectionType.Array:
         case ParameterCollectionType.ICollectionGeneric:
         {
             IEnumerator enumerator = LanguagePrimitives.GetEnumerator(parameterValue);
             bool        flag       = true;
             while (ParserOps.MoveNext(null, null, enumerator))
             {
                 object obj2 = ParserOps.Current(null, enumerator);
                 flag = false;
                 this.ValidateNullOrEmptyArgument(parameter, parameterMetadata, parameterMetadata.CollectionTypeInformation.ElementType, obj2, false);
             }
             if (flag && !parameterMetadata.AllowsEmptyCollectionArgument)
             {
                 bindingTracer.WriteLine("ERROR: Argument cannot be an empty collection", new object[0]);
                 ParameterBindingValidationException exception3 = new ParameterBindingValidationException(ErrorCategory.InvalidData, this.InvocationInfo, this.GetErrorExtent(parameter), parameterMetadata.Name, parameterMetadata.Type, (parameterValue == null) ? null : parameterValue.GetType(), "ParameterBinderStrings", (parameterMetadata.CollectionTypeInformation.ParameterCollectionType == ParameterCollectionType.Array) ? "ParameterArgumentValidationErrorEmptyArrayNotAllowed" : "ParameterArgumentValidationErrorEmptyCollectionNotAllowed", new object[0]);
                 throw exception3;
             }
             return;
         }
         }
     }
 }
Exemplo n.º 2
0
 private void ValidateNullOrEmptyArgument(CommandParameterInternal parameter, CompiledCommandParameter parameterMetadata, Type argumentType, object parameterValue, bool recurseIntoCollections)
 {
     if ((parameterValue == null) && (argumentType != typeof(bool?)))
     {
         if (!parameterMetadata.AllowsNullArgument)
         {
             bindingTracer.WriteLine("ERROR: Argument cannot be null", new object[0]);
             ParameterBindingValidationException exception = new ParameterBindingValidationException(ErrorCategory.InvalidData, this.InvocationInfo, this.GetErrorExtent(parameter), parameterMetadata.Name, argumentType, (parameterValue == null) ? null : parameterValue.GetType(), "ParameterBinderStrings", "ParameterArgumentValidationErrorNullNotAllowed", new object[0]);
             throw exception;
         }
     }
     else if (argumentType == typeof(string))
     {
         string str = parameterValue as string;
         if ((str.Length == 0) && !parameterMetadata.AllowsEmptyStringArgument)
         {
             bindingTracer.WriteLine("ERROR: Argument cannot be an empty string", new object[0]);
             ParameterBindingValidationException exception2 = new ParameterBindingValidationException(ErrorCategory.InvalidData, this.InvocationInfo, this.GetErrorExtent(parameter), parameterMetadata.Name, parameterMetadata.Type, (parameterValue == null) ? null : parameterValue.GetType(), "ParameterBinderStrings", "ParameterArgumentValidationErrorEmptyStringNotAllowed", new object[0]);
             throw exception2;
         }
     }
     else if (recurseIntoCollections)
     {
         switch (parameterMetadata.CollectionTypeInformation.ParameterCollectionType)
         {
             case ParameterCollectionType.IList:
             case ParameterCollectionType.Array:
             case ParameterCollectionType.ICollectionGeneric:
             {
                 IEnumerator enumerator = LanguagePrimitives.GetEnumerator(parameterValue);
                 bool flag = true;
                 while (ParserOps.MoveNext(null, null, enumerator))
                 {
                     object obj2 = ParserOps.Current(null, enumerator);
                     flag = false;
                     this.ValidateNullOrEmptyArgument(parameter, parameterMetadata, parameterMetadata.CollectionTypeInformation.ElementType, obj2, false);
                 }
                 if (flag && !parameterMetadata.AllowsEmptyCollectionArgument)
                 {
                     bindingTracer.WriteLine("ERROR: Argument cannot be an empty collection", new object[0]);
                     ParameterBindingValidationException exception3 = new ParameterBindingValidationException(ErrorCategory.InvalidData, this.InvocationInfo, this.GetErrorExtent(parameter), parameterMetadata.Name, parameterMetadata.Type, (parameterValue == null) ? null : parameterValue.GetType(), "ParameterBinderStrings", (parameterMetadata.CollectionTypeInformation.ParameterCollectionType == ParameterCollectionType.Array) ? "ParameterArgumentValidationErrorEmptyArrayNotAllowed" : "ParameterArgumentValidationErrorEmptyCollectionNotAllowed", new object[0]);
                     throw exception3;
                 }
                 return;
             }
         }
     }
 }
Exemplo n.º 3
0
 internal virtual bool BindParameter(CommandParameterInternal parameter, CompiledCommandParameter parameterMetadata, ParameterBindingFlags flags)
 {
     bool flag = false;
     bool flag2 = (flags & ParameterBindingFlags.ShouldCoerceType) != ParameterBindingFlags.None;
     if (parameter == null)
     {
         throw PSTraceSource.NewArgumentNullException("parameter");
     }
     if (parameterMetadata == null)
     {
         throw PSTraceSource.NewArgumentNullException("parameterMetadata");
     }
     using (bindingTracer.TraceScope("BIND arg [{0}] to parameter [{1}]", new object[] { parameter.ArgumentValue, parameterMetadata.Name }))
     {
         parameter.ParameterName = parameterMetadata.Name;
         object argumentValue = parameter.ArgumentValue;
         ScriptParameterBinder binder = this as ScriptParameterBinder;
         bool bindingScriptCmdlet = false;
         if (binder != null)
         {
             bindingScriptCmdlet = binder.Script.UsesCmdletBinding;
         }
         foreach (ArgumentTransformationAttribute attribute in parameterMetadata.ArgumentTransformationAttributes)
         {
             using (bindingTracer.TraceScope("Executing DATA GENERATION metadata: [{0}]", new object[] { attribute.GetType() }))
             {
                 try
                 {
                     ArgumentTypeConverterAttribute attribute2 = attribute as ArgumentTypeConverterAttribute;
                     if (attribute2 != null)
                     {
                         if (flag2)
                         {
                             argumentValue = attribute2.Transform(this.engine, argumentValue, true, bindingScriptCmdlet);
                         }
                     }
                     else
                     {
                         argumentValue = attribute.Transform(this.engine, argumentValue);
                     }
                     bindingTracer.WriteLine("result returned from DATA GENERATION: {0}", new object[] { argumentValue });
                 }
                 catch (Exception exception)
                 {
                     CommandProcessorBase.CheckForSevereException(exception);
                     bindingTracer.WriteLine("ERROR: DATA GENERATION: {0}", new object[] { exception.Message });
                     ParameterBindingException exception2 = new ParameterBindingArgumentTransformationException(exception, ErrorCategory.InvalidData, this.InvocationInfo, this.GetErrorExtent(parameter), parameterMetadata.Name, parameterMetadata.Type, (argumentValue == null) ? null : argumentValue.GetType(), "ParameterBinderStrings", "ParameterArgumentTransformationError", new object[] { exception.Message });
                     throw exception2;
                 }
             }
         }
         if (flag2)
         {
             argumentValue = this.CoerceTypeAsNeeded(parameter, parameterMetadata.Name, parameterMetadata.Type, parameterMetadata.CollectionTypeInformation, argumentValue);
         }
         else if (!this.ShouldContinueUncoercedBind(parameter, parameterMetadata, flags, ref argumentValue))
         {
             goto Label_040E;
         }
         if ((parameterMetadata.PSTypeName != null) && (argumentValue != null))
         {
             IEnumerable enumerable = LanguagePrimitives.GetEnumerable(argumentValue);
             if (enumerable != null)
             {
                 foreach (object obj3 in enumerable)
                 {
                     this.ValidatePSTypeName(parameter, parameterMetadata, !flag2, obj3);
                 }
             }
             else
             {
                 this.ValidatePSTypeName(parameter, parameterMetadata, !flag2, argumentValue);
             }
         }
         if ((flags & ParameterBindingFlags.IsDefaultValue) == ParameterBindingFlags.None)
         {
             foreach (ValidateArgumentsAttribute attribute3 in parameterMetadata.ValidationAttributes)
             {
                 using (bindingTracer.TraceScope("Executing VALIDATION metadata: [{0}]", new object[] { attribute3.GetType() }))
                 {
                     try
                     {
                         attribute3.InternalValidate(argumentValue, this.engine);
                     }
                     catch (Exception exception3)
                     {
                         CommandProcessorBase.CheckForSevereException(exception3);
                         bindingTracer.WriteLine("ERROR: VALIDATION FAILED: {0}", new object[] { exception3.Message });
                         ParameterBindingValidationException exception4 = new ParameterBindingValidationException(exception3, ErrorCategory.InvalidData, this.InvocationInfo, this.GetErrorExtent(parameter), parameterMetadata.Name, parameterMetadata.Type, (argumentValue == null) ? null : argumentValue.GetType(), "ParameterBinderStrings", "ParameterArgumentValidationError", new object[] { exception3.Message });
                         throw exception4;
                     }
                     tracer.WriteLine("Validation attribute on {0} returned {1}.", new object[] { parameterMetadata.Name, flag });
                 }
             }
             if (IsParameterMandatory(parameterMetadata))
             {
                 this.ValidateNullOrEmptyArgument(parameter, parameterMetadata, parameterMetadata.Type, argumentValue, true);
             }
         }
         Exception innerException = null;
         try
         {
             this.BindParameter(parameter.ParameterName, argumentValue);
             flag = true;
         }
         catch (SetValueException exception6)
         {
             innerException = exception6;
         }
         if (innerException != null)
         {
             Type typeSpecified = (argumentValue == null) ? null : argumentValue.GetType();
             ParameterBindingException exception7 = new ParameterBindingException(innerException, ErrorCategory.WriteError, this.InvocationInfo, this.GetErrorExtent(parameter), parameterMetadata.Name, parameterMetadata.Type, typeSpecified, "ParameterBinderStrings", "ParameterBindingFailed", new object[] { innerException.Message });
             throw exception7;
         }
     Label_040E:;
         bindingTracer.WriteLine("BIND arg [{0}] to param [{1}] {2}", new object[] { argumentValue, parameter.ParameterName, flag ? "SUCCESSFUL" : "SKIPPED" });
         if (flag)
         {
             if (this.RecordBoundParameters)
             {
                 this.CommandLineParameters.Add(parameter.ParameterName, argumentValue);
             }
             MshCommandRuntime commandRuntime = this.Command.commandRuntime as MshCommandRuntime;
             if ((commandRuntime != null) && commandRuntime.LogPipelineExecutionDetail)
             {
                 IEnumerable source = LanguagePrimitives.GetEnumerable(argumentValue);
                 if (source != null)
                 {
                     string parameterValue = string.Join(", ", source.Cast<object>().ToArray<object>());
                     commandRuntime.PipelineProcessor.LogExecutionParameterBinding(this.InvocationInfo, parameter.ParameterName, parameterValue);
                 }
                 else
                 {
                     commandRuntime.PipelineProcessor.LogExecutionParameterBinding(this.InvocationInfo, parameter.ParameterName, (argumentValue == null) ? "" : argumentValue.ToString());
                 }
             }
         }
         return flag;
     }
 }
Exemplo n.º 4
0
        internal virtual bool BindParameter(CommandParameterInternal parameter, CompiledCommandParameter parameterMetadata, ParameterBindingFlags flags)
        {
            bool flag  = false;
            bool flag2 = (flags & ParameterBindingFlags.ShouldCoerceType) != ParameterBindingFlags.None;

            if (parameter == null)
            {
                throw PSTraceSource.NewArgumentNullException("parameter");
            }
            if (parameterMetadata == null)
            {
                throw PSTraceSource.NewArgumentNullException("parameterMetadata");
            }
            using (bindingTracer.TraceScope("BIND arg [{0}] to parameter [{1}]", new object[] { parameter.ArgumentValue, parameterMetadata.Name }))
            {
                parameter.ParameterName = parameterMetadata.Name;
                object argumentValue         = parameter.ArgumentValue;
                ScriptParameterBinder binder = this as ScriptParameterBinder;
                bool bindingScriptCmdlet     = false;
                if (binder != null)
                {
                    bindingScriptCmdlet = binder.Script.UsesCmdletBinding;
                }
                foreach (ArgumentTransformationAttribute attribute in parameterMetadata.ArgumentTransformationAttributes)
                {
                    using (bindingTracer.TraceScope("Executing DATA GENERATION metadata: [{0}]", new object[] { attribute.GetType() }))
                    {
                        try
                        {
                            ArgumentTypeConverterAttribute attribute2 = attribute as ArgumentTypeConverterAttribute;
                            if (attribute2 != null)
                            {
                                if (flag2)
                                {
                                    argumentValue = attribute2.Transform(this.engine, argumentValue, true, bindingScriptCmdlet);
                                }
                            }
                            else
                            {
                                argumentValue = attribute.Transform(this.engine, argumentValue);
                            }
                            bindingTracer.WriteLine("result returned from DATA GENERATION: {0}", new object[] { argumentValue });
                        }
                        catch (Exception exception)
                        {
                            CommandProcessorBase.CheckForSevereException(exception);
                            bindingTracer.WriteLine("ERROR: DATA GENERATION: {0}", new object[] { exception.Message });
                            ParameterBindingException exception2 = new ParameterBindingArgumentTransformationException(exception, ErrorCategory.InvalidData, this.InvocationInfo, this.GetErrorExtent(parameter), parameterMetadata.Name, parameterMetadata.Type, (argumentValue == null) ? null : argumentValue.GetType(), "ParameterBinderStrings", "ParameterArgumentTransformationError", new object[] { exception.Message });
                            throw exception2;
                        }
                    }
                }
                if (flag2)
                {
                    argumentValue = this.CoerceTypeAsNeeded(parameter, parameterMetadata.Name, parameterMetadata.Type, parameterMetadata.CollectionTypeInformation, argumentValue);
                }
                else if (!this.ShouldContinueUncoercedBind(parameter, parameterMetadata, flags, ref argumentValue))
                {
                    goto Label_040E;
                }
                if ((parameterMetadata.PSTypeName != null) && (argumentValue != null))
                {
                    IEnumerable enumerable = LanguagePrimitives.GetEnumerable(argumentValue);
                    if (enumerable != null)
                    {
                        foreach (object obj3 in enumerable)
                        {
                            this.ValidatePSTypeName(parameter, parameterMetadata, !flag2, obj3);
                        }
                    }
                    else
                    {
                        this.ValidatePSTypeName(parameter, parameterMetadata, !flag2, argumentValue);
                    }
                }
                if ((flags & ParameterBindingFlags.IsDefaultValue) == ParameterBindingFlags.None)
                {
                    foreach (ValidateArgumentsAttribute attribute3 in parameterMetadata.ValidationAttributes)
                    {
                        using (bindingTracer.TraceScope("Executing VALIDATION metadata: [{0}]", new object[] { attribute3.GetType() }))
                        {
                            try
                            {
                                attribute3.InternalValidate(argumentValue, this.engine);
                            }
                            catch (Exception exception3)
                            {
                                CommandProcessorBase.CheckForSevereException(exception3);
                                bindingTracer.WriteLine("ERROR: VALIDATION FAILED: {0}", new object[] { exception3.Message });
                                ParameterBindingValidationException exception4 = new ParameterBindingValidationException(exception3, ErrorCategory.InvalidData, this.InvocationInfo, this.GetErrorExtent(parameter), parameterMetadata.Name, parameterMetadata.Type, (argumentValue == null) ? null : argumentValue.GetType(), "ParameterBinderStrings", "ParameterArgumentValidationError", new object[] { exception3.Message });
                                throw exception4;
                            }
                            tracer.WriteLine("Validation attribute on {0} returned {1}.", new object[] { parameterMetadata.Name, flag });
                        }
                    }
                    if (IsParameterMandatory(parameterMetadata))
                    {
                        this.ValidateNullOrEmptyArgument(parameter, parameterMetadata, parameterMetadata.Type, argumentValue, true);
                    }
                }
                Exception innerException = null;
                try
                {
                    this.BindParameter(parameter.ParameterName, argumentValue);
                    flag = true;
                }
                catch (SetValueException exception6)
                {
                    innerException = exception6;
                }
                if (innerException != null)
                {
                    Type typeSpecified = (argumentValue == null) ? null : argumentValue.GetType();
                    ParameterBindingException exception7 = new ParameterBindingException(innerException, ErrorCategory.WriteError, this.InvocationInfo, this.GetErrorExtent(parameter), parameterMetadata.Name, parameterMetadata.Type, typeSpecified, "ParameterBinderStrings", "ParameterBindingFailed", new object[] { innerException.Message });
                    throw exception7;
                }
                Label_040E :;
                bindingTracer.WriteLine("BIND arg [{0}] to param [{1}] {2}", new object[] { argumentValue, parameter.ParameterName, flag ? "SUCCESSFUL" : "SKIPPED" });
                if (flag)
                {
                    if (this.RecordBoundParameters)
                    {
                        this.CommandLineParameters.Add(parameter.ParameterName, argumentValue);
                    }
                    MshCommandRuntime commandRuntime = this.Command.commandRuntime as MshCommandRuntime;
                    if ((commandRuntime != null) && commandRuntime.LogPipelineExecutionDetail)
                    {
                        IEnumerable source = LanguagePrimitives.GetEnumerable(argumentValue);
                        if (source != null)
                        {
                            string parameterValue = string.Join(", ", source.Cast <object>().ToArray <object>());
                            commandRuntime.PipelineProcessor.LogExecutionParameterBinding(this.InvocationInfo, parameter.ParameterName, parameterValue);
                        }
                        else
                        {
                            commandRuntime.PipelineProcessor.LogExecutionParameterBinding(this.InvocationInfo, parameter.ParameterName, (argumentValue == null) ? "" : argumentValue.ToString());
                        }
                    }
                }
                return(flag);
            }
        }