// 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 { /// /// Provides a description of a field for use by . /// /// /// /// It is permitted to subclass /// but there is no established scenario for doing this, nor has it been tested. /// public class FieldDescription { /// /// Initializes a new instance of FieldDescription and defines the Name value. /// /// /// The name to identify this field description /// /// /// is null or empty. /// 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; } /// /// Gets the name of the field. /// public string Name { get { return name; } } /// /// Sets the ParameterTypeName, ParameterTypeFullName, and ParameterAssemblyFullName as a single operation. /// /// /// The Type that sets the properties. /// /// /// If is null. /// public void SetParameterType(System.Type parameterType) { if (parameterType == null) { throw PSTraceSource.NewArgumentNullException(nameof(parameterType)); } SetParameterTypeName(parameterType.Name); SetParameterTypeFullName(parameterType.FullName); SetParameterAssemblyFullName(parameterType.AssemblyQualifiedName); } /// /// Gets the short name of the parameter's type. /// /// /// The type name of the parameter /// /// /// If not already set by a call to , /// will be used as the type. /// /// public string ParameterTypeName { get { if (string.IsNullOrEmpty(parameterTypeName)) { // the default if the type name is not specified is 'string' SetParameterType(typeof(string)); } return parameterTypeName; } } /// /// Gets the full string name of the parameter's type. /// /// /// If not already set by a call to , /// will be used as the type. /// /// public string ParameterTypeFullName { get { if (string.IsNullOrEmpty(parameterTypeFullName)) { // the default if the type name is not specified is 'string' SetParameterType(typeof(string)); } return parameterTypeFullName; } } /// /// Gets the full name of the assembly containing the type identified by ParameterTypeFullName or ParameterTypeName. /// /// /// 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 , /// will be used as the type. /// public string ParameterAssemblyFullName { get { if (string.IsNullOrEmpty(parameterAssemblyFullName)) { // the default if the type name is not specified is 'string' SetParameterType(typeof(string)); } return parameterAssemblyFullName; } } /// /// A short, human-presentable message to describe and identify the field. If supplied, a typical implementation of /// will use this value instead of /// the field name to identify the field to the user. /// /// /// set to null. /// /// /// 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 /// implementation may use /// to allow the user to quickly set input focus to this field. The implementation of /// 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. /// 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; } } /// /// Gets and sets the help message for this field. /// /// /// Set to null. /// /// /// 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. /// 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; } } /// /// Gets and sets whether a value must be supplied for this field. /// public bool IsMandatory { get { return isMandatory; } set { isMandatory = value; } } /// /// Gets and sets the default value, if any, for the implementation of /// to pre-populate its UI with. This is a PSObject instance so that the value can be serialized, converted, /// manipulated like any pipeline object. /// /// /// It is up to the implementer of to decide if it /// can make use of the object in its presentation of the fields prompt. /// /// public PSObject DefaultValue { get { return defaultValue; } set { // null is allowed. defaultValue = value; } } /// /// Gets the Attribute classes that apply to the field. In the case that /// is being called from the engine, this will contain the set of prompting attributes that are attached to a /// cmdlet parameter declaration. /// public Collection Attributes { get { return metadata ??= new Collection(); } } /// /// For use by remoting serialization. /// /// /// /// If is null. /// internal void SetParameterTypeName(string nameOfType) { if (string.IsNullOrEmpty(nameOfType)) { throw PSTraceSource.NewArgumentException(nameof(nameOfType), DescriptionsStrings.NullOrEmptyErrorTemplate, "nameOfType"); } parameterTypeName = nameOfType; } /// /// For use by remoting serialization. /// /// /// /// If is null. /// internal void SetParameterTypeFullName(string fullNameOfType) { if (string.IsNullOrEmpty(fullNameOfType)) { throw PSTraceSource.NewArgumentException(nameof(fullNameOfType), DescriptionsStrings.NullOrEmptyErrorTemplate, "fullNameOfType"); } parameterTypeFullName = fullNameOfType; } /// /// For use by remoting serialization. /// /// /// /// If is null. /// internal void SetParameterAssemblyFullName(string fullNameOfAssembly) { if (string.IsNullOrEmpty(fullNameOfAssembly)) { throw PSTraceSource.NewArgumentException(nameof(fullNameOfAssembly), DescriptionsStrings.NullOrEmptyErrorTemplate, "fullNameOfAssembly"); } parameterAssemblyFullName = fullNameOfAssembly; } /// /// Indicates if this field description was /// modified by the remoting protocol layer. /// /// Used by the console host to /// determine if this field description was /// modified by the remoting protocol layer /// and take appropriate actions internal bool ModifiedByRemotingProtocol { get { return modifiedByRemotingProtocol; } set { modifiedByRemotingProtocol = value; } } /// /// Indicates if this field description /// is coming from a remote host. /// /// Used by the console host to /// not cast strings to an arbitrary type, /// but let the server-side do the type conversion /// 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 metadata = new Collection(); private bool modifiedByRemotingProtocol = false; private bool isFromRemoteHost = false; #endregion } }