MegaBites-AI/Windows-powershell
0372
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System.Management.Automation.Internal;5 6namespace System.Management.Automation7{8 /// <summary>9 /// The parameter binder for runtime-defined parameters which are declared through the RuntimeDefinedParameterDictionary.10 /// </summary>11 internal class RuntimeDefinedParameterBinder : ParameterBinderBase12 {13 #region ctor14 15 /// <summary>16 /// Constructs the parameter binder with the specified type metadata. The binder is only valid17 /// for a single instance of a bindable runtime-defined parameter collection and only for the duration of a command.18 /// </summary>19 /// <param name="target">20 /// The target runtime-defined parameter collection that the parameter values will be bound to.21 /// </param>22 /// <param name="command">23 /// An instance of the command so that attributes can access the context.24 /// </param>25 /// <param name="commandLineParameters">26 /// The Command line parameter collection to update...27 /// </param>28 internal RuntimeDefinedParameterBinder(29 RuntimeDefinedParameterDictionary target,30 InternalCommand command,31 CommandLineParameters commandLineParameters)32 : base(target, command.MyInvocation, command.Context, command)33 {34 // NTRAID#Windows Out Of Band Releases-927103-2006/01/25-JonN35 foreach (var pair in target)36 {37 string key = pair.Key;38 RuntimeDefinedParameter pp = pair.Value;39 string ppName = pp?.Name;40 if (pp == null || key != ppName)41 {42 ParameterBindingException bindingException =43 new ParameterBindingException(44 ErrorCategory.InvalidArgument,45 command.MyInvocation,46 null,47 ppName,48 null,49 null,50 ParameterBinderStrings.RuntimeDefinedParameterNameMismatch,51 "RuntimeDefinedParameterNameMismatch",52 key);53 54 throw bindingException;55 }56 }57 58 this.CommandLineParameters = commandLineParameters;59 }60 61 #endregion ctor62 63 #region internal members64 65 /// <summary>66 /// Hides the base class Target property to ensure the target67 /// is always a RuntimeDefinedParameterDictionary.68 /// </summary>69 internal new RuntimeDefinedParameterDictionary Target70 {71 get72 {73 return base.Target as RuntimeDefinedParameterDictionary;74 }75 76 set77 {78 base.Target = value;79 }80 }81 82 #region Parameter default values83 84 /// <summary>85 /// Gets the default value for the specified parameter.86 /// </summary>87 /// <param name="name">88 /// The name of the parameter to get the value for.89 /// </param>90 /// <returns>91 /// The value of the specified parameter92 /// </returns>93 internal override object GetDefaultParameterValue(string name)94 {95 object result = null;96 RuntimeDefinedParameter parameter;97 if (this.Target.TryGetValue(name, out parameter) && parameter != null)98 {99 result = parameter.Value;100 }101 102 return result;103 }104 105 #endregion Parameter default values106 107 /// <summary>108 /// Uses ETS to set the property specified by name to the value on109 /// the target bindable object.110 /// </summary>111 /// <param name="name">112 /// The name of the parameter to bind the value to.113 /// </param>114 /// <param name="value">115 /// The value to bind to the parameter. It should be assumed by116 /// derived classes that the proper type coercion has already taken117 /// place and that any prerequisite metadata has been satisfied.118 /// </param>119 /// <param name="parameterMetadata"></param>120 /// <exception cref="ArgumentException">121 /// If <paramref name="name"/> is null or empty.122 /// </exception>123 internal override void BindParameter(string name, object value, CompiledCommandParameter parameterMetadata)124 {125 if (string.IsNullOrEmpty(name))126 {127 throw PSTraceSource.NewArgumentException(nameof(name));128 }129 130 Target[name].Value = value;131 this.CommandLineParameters.Add(name, value);132 }133 134 #endregion Parameter binding135 }136}137 