/********************************************************************++
Copyright (c) Microsoft Corporation. All rights reserved.
--********************************************************************/
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("name", DescriptionsStrings.NullOrEmptyErrorTemplate, "name");
}
Name = name;
}
///
/// Added to enable ClrFacade.GetUninitializedObject to instantiate an uninitialized version of this class.
///
internal
FieldDescription()
{ }
///
/// Gets the name of the field.
///
public string Name { get; } = null;
///
///
/// 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("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; set; } = true;
///
///
/// 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; set; } = null;
///
///
/// Gets the Attribute classes that apply to the field. In the case that
/// is being called from the MSH engine, this will contain the set of prompting attributes that are attached to a
/// cmdlet parameter declaration.
///
///
public
Collection
Attributes
{
get { return _metadata ?? (_metadata = new Collection()); }
}
///
///
/// For use by remoting serialization.
///
///
///
///
///
/// If is null.
///
///
internal
void
SetParameterTypeName(string nameOfType)
{
if (String.IsNullOrEmpty(nameOfType))
{
throw PSTraceSource.NewArgumentException("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("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("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; set; } = false;
///
/// 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; set; } = false;
#region Helper
#endregion Helper
private string _label = "";
private string _parameterTypeName = null;
private string _parameterTypeFullName = null;
private string _parameterAssemblyFullName = null;
private string _helpMessage = "";
private Collection _metadata = new Collection();
}
}