private void CheckAndAddProperty(PSPropertyInfo propertyInfo, Attribute[] attributes, ref PropertyDescriptorCollection returnValue) { using (typeDescriptor.TraceScope("Checking property \"{0}\".", propertyInfo.Name)) { // WriteOnly properties are not returned in TypeDescriptor.GetProperties, so we do the same. if (!propertyInfo.IsGettable) { typeDescriptor.WriteLine("Property \"{0}\" is write-only so it has been skipped.", propertyInfo.Name); return; } AttributeCollection propertyAttributes = null; Type propertyType = typeof(object); if (attributes != null && attributes.Length != 0) { PSProperty property = propertyInfo as PSProperty; if (property != null) { DotNetAdapter.PropertyCacheEntry propertyEntry = property.adapterData as DotNetAdapter.PropertyCacheEntry; if (propertyEntry == null) { typeDescriptor.WriteLine("Skipping attribute check for property \"{0}\" because it is an adapted property (not a .NET property).", property.Name); } else if (property.isDeserialized) { // At the moment we are not serializing attributes, so we can skip // the attribute check if the property is deserialized. typeDescriptor.WriteLine("Skipping attribute check for property \"{0}\" because it has been deserialized.", property.Name); } else { propertyType = propertyEntry.propertyType; propertyAttributes = propertyEntry.Attributes; foreach (Attribute attribute in attributes) { if (!propertyAttributes.Contains(attribute)) { typeDescriptor.WriteLine("Property \"{0}\" does not contain attribute \"{1}\" so it has been skipped.", property.Name, attribute); return; } } } } } if (propertyAttributes == null) { propertyAttributes = new AttributeCollection(); } typeDescriptor.WriteLine("Adding property \"{0}\".", propertyInfo.Name); PSObjectPropertyDescriptor propertyDescriptor = new PSObjectPropertyDescriptor(propertyInfo.Name, propertyType, !propertyInfo.IsSettable, propertyAttributes); propertyDescriptor.SettingValueException += this.SettingValueException; propertyDescriptor.GettingValueException += this.GettingValueException; returnValue.Add(propertyDescriptor); } }
private void CheckAndAddProperty(PSPropertyInfo propertyInfo, Attribute[] attributes, ref PropertyDescriptorCollection returnValue) { using (typeDescriptor.TraceScope("Checking property \"{0}\".", new object[] { propertyInfo.Name })) { if (!propertyInfo.IsGettable) { typeDescriptor.WriteLine("Property \"{0}\" is write-only so it has been skipped.", new object[] { propertyInfo.Name }); } else { AttributeCollection propertyAttributes = null; Type propertyType = typeof(object); if ((attributes != null) && (attributes.Length != 0)) { PSProperty property = propertyInfo as PSProperty; if (property != null) { DotNetAdapter.PropertyCacheEntry adapterData = property.adapterData as DotNetAdapter.PropertyCacheEntry; if (adapterData == null) { typeDescriptor.WriteLine("Skipping attribute check for property \"{0}\" because it is an adapted property (not a .NET property).", new object[] { property.Name }); } else if (property.isDeserialized) { typeDescriptor.WriteLine("Skipping attribute check for property \"{0}\" because it has been deserialized.", new object[] { property.Name }); } else { propertyType = adapterData.propertyType; propertyAttributes = adapterData.Attributes; foreach (Attribute attribute in attributes) { if (!propertyAttributes.Contains(attribute)) { typeDescriptor.WriteLine("Property \"{0}\" does not contain attribute \"{1}\" so it has been skipped.", new object[] { property.Name, attribute }); return; } } } } } if (propertyAttributes == null) { propertyAttributes = new AttributeCollection(new Attribute[0]); } typeDescriptor.WriteLine("Adding property \"{0}\".", new object[] { propertyInfo.Name }); PSObjectPropertyDescriptor descriptor = new PSObjectPropertyDescriptor(propertyInfo.Name, propertyType, !propertyInfo.IsSettable, propertyAttributes); descriptor.SettingValueException += this.SettingValueException; descriptor.GettingValueException += this.GettingValueException; returnValue.Add(descriptor); } } }
private static MethodInfo Infer(MethodInfo genericMethod, ICollection <Type> typeParameters, IEnumerable <Type> typesOfMethodParameters, IEnumerable <Type> typesOfMethodArguments) { MethodInfo info2; using (tracer.TraceScope("Inferring type parameters for the following method: {0}", new object[] { genericMethod })) { if (PSTraceSourceOptions.WriteLine == (tracer.Options & PSTraceSourceOptions.WriteLine)) { object[] args = new object[] { string.Join(", ", (from t in typesOfMethodArguments select t.ToString()).ToArray <string>()) }; tracer.WriteLine("Types of method arguments: {0}", args); } TypeInference inference = new TypeInference(typeParameters); if (!inference.UnifyMultipleTerms(typesOfMethodParameters, typesOfMethodArguments)) { return(null); } IEnumerable <Type> source = typeParameters.Select <Type, Type>(new Func <Type, Type>(inference.GetInferredType)); if (source.Any <Type>(inferredType => inferredType == null)) { info2 = null; } else { try { MethodInfo info = genericMethod.MakeGenericMethod(source.ToArray <Type>()); tracer.WriteLine("Inference succesful: {0}", new object[] { info }); info2 = info; } catch (ArgumentException exception) { tracer.WriteLine("Inference failure: {0}", new object[] { exception.Message }); info2 = null; } } } return(info2); }
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); } }