File size: 14,121 Bytes
8c763fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Collections.ObjectModel;
using Dbg = System.Management.Automation.Diagnostics;
//using System.Runtime.Serialization;
//using System.ComponentModel;
//using System.Runtime.InteropServices;
//using System.Globalization;
//using System.Management.Automation;
//using System.Reflection;
namespace System.Management.Automation.Host
{
/// <summary>
/// Provides a description of a field for use by <see cref="System.Management.Automation.Host.PSHostUserInterface.Prompt"/>.
/// <!--Used by the engine to describe cmdlet parameters.-->
/// </summary>
/// <remarks>
/// It is permitted to subclass <see cref="System.Management.Automation.Host.FieldDescription"/>
/// but there is no established scenario for doing this, nor has it been tested.
/// </remarks>
public class
FieldDescription
{
/// <summary>
/// Initializes a new instance of FieldDescription and defines the Name value.
/// </summary>
/// <param name="name">
/// The name to identify this field description
/// </param>
/// <exception cref="System.Management.Automation.PSArgumentException">
/// <paramref name="name"/> is null or empty.
/// </exception>
public
FieldDescription(string name)
{
// the only required parameter is the name.
if (string.IsNullOrEmpty(name))
{
throw PSTraceSource.NewArgumentException(nameof(name), DescriptionsStrings.NullOrEmptyErrorTemplate, "name");
}
this.name = name;
}
/// <summary>
/// Gets the name of the field.
/// </summary>
public string Name
{
get
{
return name;
}
}
/// <summary>
/// Sets the ParameterTypeName, ParameterTypeFullName, and ParameterAssemblyFullName as a single operation.
/// </summary>
/// <param name="parameterType">
/// The Type that sets the properties.
/// </param>
/// <exception cref="System.Management.Automation.PSArgumentNullException">
/// If <paramref name="parameterType"/> is null.
/// </exception>
public
void
SetParameterType(System.Type parameterType)
{
if (parameterType == null)
{
throw PSTraceSource.NewArgumentNullException(nameof(parameterType));
}
SetParameterTypeName(parameterType.Name);
SetParameterTypeFullName(parameterType.FullName);
SetParameterAssemblyFullName(parameterType.AssemblyQualifiedName);
}
/// <summary>
/// Gets the short name of the parameter's type.
/// </summary>
/// <value>
/// The type name of the parameter
/// </value>
/// <remarks>
/// If not already set by a call to <see cref="System.Management.Automation.Host.FieldDescription.SetParameterType"/>,
/// <see cref="string"/> will be used as the type.
/// <!--The value of ParameterTypeName is the string value returned.
/// by System.Type.Name.-->
/// </remarks>
public
string
ParameterTypeName
{
get
{
if (string.IsNullOrEmpty(parameterTypeName))
{
// the default if the type name is not specified is 'string'
SetParameterType(typeof(string));
}
return parameterTypeName;
}
}
/// <summary>
/// Gets the full string name of the parameter's type.
/// </summary>
/// <remarks>
/// If not already set by a call to <see cref="System.Management.Automation.Host.FieldDescription.SetParameterType"/>,
/// <see cref="string"/> will be used as the type.
/// <!--The value of ParameterTypeName is the string value returned.
/// by System.Type.Name.-->
/// </remarks>
public
string
ParameterTypeFullName
{
get
{
if (string.IsNullOrEmpty(parameterTypeFullName))
{
// the default if the type name is not specified is 'string'
SetParameterType(typeof(string));
}
return parameterTypeFullName;
}
}
/// <summary>
/// Gets the full name of the assembly containing the type identified by ParameterTypeFullName or ParameterTypeName.
/// </summary>
/// <remarks>
/// If the assembly is not currently loaded in the hosting application's AppDomain, the hosting application needs
/// to load the containing assembly to access the type information. AssemblyName is used for this purpose.
///
/// If not already set by a call to <see cref="System.Management.Automation.Host.FieldDescription.SetParameterType"/>,
/// <see cref="string"/> will be used as the type.
/// </remarks>
public
string
ParameterAssemblyFullName
{
get
{
if (string.IsNullOrEmpty(parameterAssemblyFullName))
{
// the default if the type name is not specified is 'string'
SetParameterType(typeof(string));
}
return parameterAssemblyFullName;
}
}
/// <summary>
/// A short, human-presentable message to describe and identify the field. If supplied, a typical implementation of
/// <see cref="System.Management.Automation.Host.PSHostUserInterface.Prompt"/> will use this value instead of
/// the field name to identify the field to the user.
/// </summary>
/// <exception cref="System.Management.Automation.PSArgumentNullException">
/// set to null.
/// </exception>
/// <remarks>
/// Note that the special character & (ampersand) may be embedded in the label string to identify the next
/// character in the label as a "hot key" (aka "keyboard accelerator") that the
/// <see cref="System.Management.Automation.Host.PSHostUserInterface.Prompt"/> implementation may use
/// to allow the user to quickly set input focus to this field. The implementation of
/// <see cref="System.Management.Automation.Host.PSHostUserInterface.Prompt"/> is responsible for parsing
/// the label string for this special character and rendering it accordingly.
///
/// For example, a field named "SSN" might have "&Social Security Number" as it's label.
///
/// If no label is set, then the empty string is returned.
/// </remarks>
public
string
Label
{
get
{
Dbg.Assert(label != null, "label should not be null");
return label;
}
set
{
if (value == null)
{
throw PSTraceSource.NewArgumentNullException("value");
}
label = value;
}
}
/// <summary>
/// Gets and sets the help message for this field.
/// </summary>
/// <exception cref="System.Management.Automation.PSArgumentNullException">
/// Set to null.
/// </exception>
/// <remarks>
/// This should be a few sentences to describe the field, suitable for presentation as a tool tip.
/// Avoid placing including formatting characters such as newline and tab.
/// </remarks>
public
string
HelpMessage
{
get
{
Dbg.Assert(helpMessage != null, "helpMessage should not be null");
return helpMessage;
}
set
{
if (value == null)
{
throw PSTraceSource.NewArgumentNullException("value");
}
helpMessage = value;
}
}
/// <summary>
/// Gets and sets whether a value must be supplied for this field.
/// </summary>
public
bool
IsMandatory
{
get
{
return isMandatory;
}
set
{
isMandatory = value;
}
}
/// <summary>
/// Gets and sets the default value, if any, for the implementation of <see cref="System.Management.Automation.Host.PSHostUserInterface.Prompt"/>
/// to pre-populate its UI with. This is a PSObject instance so that the value can be serialized, converted,
/// manipulated like any pipeline object.
/// </summary>
/// <remarks>
/// It is up to the implementer of <see cref="System.Management.Automation.Host.PSHostUserInterface.Prompt"/> to decide if it
/// can make use of the object in its presentation of the fields prompt.
///
/// </remarks>
public
PSObject
DefaultValue
{
get
{
return defaultValue;
}
set
{
// null is allowed.
defaultValue = value;
}
}
/// <summary>
/// Gets the Attribute classes that apply to the field. In the case that <see cref="System.Management.Automation.Host.PSHostUserInterface.Prompt"/>
/// is being called from the engine, this will contain the set of prompting attributes that are attached to a
/// cmdlet parameter declaration.
/// </summary>
public
Collection<Attribute>
Attributes
{
get { return metadata ??= new Collection<Attribute>(); }
}
/// <summary>
/// For use by remoting serialization.
/// </summary>
/// <param name="nameOfType"></param>
/// <exception cref="System.Management.Automation.PSArgumentException">
/// If <paramref name="nameOfType"/> is null.
/// </exception>
internal
void
SetParameterTypeName(string nameOfType)
{
if (string.IsNullOrEmpty(nameOfType))
{
throw PSTraceSource.NewArgumentException(nameof(nameOfType), DescriptionsStrings.NullOrEmptyErrorTemplate, "nameOfType");
}
parameterTypeName = nameOfType;
}
/// <summary>
/// For use by remoting serialization.
/// </summary>
/// <param name="fullNameOfType"></param>
/// <exception cref="System.Management.Automation.PSArgumentException">
/// If <paramref name="fullNameOfType"/> is null.
/// </exception>
internal
void
SetParameterTypeFullName(string fullNameOfType)
{
if (string.IsNullOrEmpty(fullNameOfType))
{
throw PSTraceSource.NewArgumentException(nameof(fullNameOfType), DescriptionsStrings.NullOrEmptyErrorTemplate, "fullNameOfType");
}
parameterTypeFullName = fullNameOfType;
}
/// <summary>
/// For use by remoting serialization.
/// </summary>
/// <param name="fullNameOfAssembly"></param>
/// <exception cref="System.Management.Automation.PSArgumentException">
/// If <paramref name="fullNameOfAssembly"/> is null.
/// </exception>
internal
void
SetParameterAssemblyFullName(string fullNameOfAssembly)
{
if (string.IsNullOrEmpty(fullNameOfAssembly))
{
throw PSTraceSource.NewArgumentException(nameof(fullNameOfAssembly), DescriptionsStrings.NullOrEmptyErrorTemplate, "fullNameOfAssembly");
}
parameterAssemblyFullName = fullNameOfAssembly;
}
/// <summary>
/// Indicates if this field description was
/// modified by the remoting protocol layer.
/// </summary>
/// <remarks>Used by the console host to
/// determine if this field description was
/// modified by the remoting protocol layer
/// and take appropriate actions</remarks>
internal bool ModifiedByRemotingProtocol
{
get
{
return modifiedByRemotingProtocol;
}
set
{
modifiedByRemotingProtocol = value;
}
}
/// <summary>
/// Indicates if this field description
/// is coming from a remote host.
/// </summary>
/// <remarks>Used by the console host to
/// not cast strings to an arbitrary type,
/// but let the server-side do the type conversion
/// </remarks>
internal bool IsFromRemoteHost
{
get
{
return isFromRemoteHost;
}
set
{
isFromRemoteHost = value;
}
}
#region Helper
#endregion Helper
#region DO NOT REMOVE OR RENAME THESE FIELDS - it will break remoting compatibility with Windows PowerShell
private readonly string name = null;
private string label = string.Empty;
private string parameterTypeName = null;
private string parameterTypeFullName = null;
private string parameterAssemblyFullName = null;
private string helpMessage = string.Empty;
private bool isMandatory = true;
private PSObject defaultValue = null;
private Collection<Attribute> metadata = new Collection<Attribute>();
private bool modifiedByRemotingProtocol = false;
private bool isFromRemoteHost = false;
#endregion
}
}
|