// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Management.Automation.Internal;
using System.Management.Automation.Language;
using System.Runtime.Serialization;
using System.Security.Permissions;
namespace System.Management.Automation
{
///
/// The exception thrown if the specified value can not be bound parameter of a command.
///
[Serializable]
public class ParameterBindingException : RuntimeException
{
#region Constructors
#region Preferred constructors
///
/// Constructs a ParameterBindingException.
///
///
/// The category for the error.
///
///
/// The information about the command that encountered the error.
///
///
///
/// The position for the command or parameter that caused the error.
/// If position is null, the one from the InvocationInfo is used.
///
///
///
/// The parameter on which binding caused the error.
///
///
///
/// The Type the parameter was expecting.
///
///
///
/// The Type that was attempted to be bound to the parameter.
///
///
///
/// The format string for the exception message.
///
///
/// The error ID.
///
///
/// Additional arguments to pass to the format string.
///
///
///
/// If or
/// is null or empty.
///
internal ParameterBindingException(
ErrorCategory errorCategory,
InvocationInfo invocationInfo,
IScriptExtent errorPosition,
string parameterName,
Type parameterType,
Type typeSpecified,
string resourceString,
string errorId,
params object[] args)
: base(errorCategory, invocationInfo, errorPosition, errorId, null, null)
{
if (string.IsNullOrEmpty(resourceString))
{
throw PSTraceSource.NewArgumentException("resourceString");
}
if (string.IsNullOrEmpty(errorId))
{
throw PSTraceSource.NewArgumentException("errorId");
}
_invocationInfo = invocationInfo;
if (_invocationInfo != null)
{
_commandName = invocationInfo.MyCommand.Name;
}
_parameterName = parameterName;
_parameterType = parameterType;
_typeSpecified = typeSpecified;
if ((errorPosition == null) && (_invocationInfo != null))
{
errorPosition = invocationInfo.ScriptPosition;
}
if (errorPosition != null)
{
_line = errorPosition.StartLineNumber;
_offset = errorPosition.StartColumnNumber;
}
_resourceString = resourceString;
_errorId = errorId;
if (args != null)
{
_args = args;
}
}
///
/// Constructs a ParameterBindingException.
///
///
/// The inner exception.
///
///
/// The category for the error.
///
///
/// The information about the command that encountered the error.
///
/// InvocationInfo.MyCommand.Name == {0}
///
///
/// The position for the command or parameter that caused the error.
/// If position is null, the one from the InvocationInfo is used.
///
/// token.LineNumber == {4}
/// token.OffsetInLine == {5}
///
///
/// The parameter on which binding caused the error.
///
/// parameterName == {1}
///
///
/// The Type the parameter was expecting.
///
/// parameterType == {2}
///
///
/// The Type that was attempted to be bound to the parameter.
///
/// typeSpecified == {3}
///
///
/// The format string for the exception message.
///
///
/// The error ID.
///
///
/// Additional arguments to pass to the format string.
///
/// starts at {6}
///
///
/// If is null.
///
///
/// If or
/// is null or empty.
///
internal ParameterBindingException(
Exception innerException,
ErrorCategory errorCategory,
InvocationInfo invocationInfo,
IScriptExtent errorPosition,
string parameterName,
Type parameterType,
Type typeSpecified,
string resourceString,
string errorId,
params object[] args)
: base(errorCategory, invocationInfo, errorPosition, errorId, null, innerException)
{
if (invocationInfo == null)
{
throw PSTraceSource.NewArgumentNullException("invocationInfo");
}
if (string.IsNullOrEmpty(resourceString))
{
throw PSTraceSource.NewArgumentException("resourceString");
}
if (string.IsNullOrEmpty(errorId))
{
throw PSTraceSource.NewArgumentException("errorId");
}
_invocationInfo = invocationInfo;
_commandName = invocationInfo.MyCommand.Name;
_parameterName = parameterName;
_parameterType = parameterType;
_typeSpecified = typeSpecified;
if (errorPosition == null)
{
errorPosition = invocationInfo.ScriptPosition;
}
if (errorPosition != null)
{
_line = errorPosition.StartLineNumber;
_offset = errorPosition.StartColumnNumber;
}
_resourceString = resourceString;
_errorId = errorId;
if (args != null)
{
_args = args;
}
}
///
///
///
///
///
///
internal ParameterBindingException(
Exception innerException,
ParameterBindingException pbex,
string resourceString,
params object[] args)
: base(string.Empty, innerException)
{
if (pbex == null)
{
throw PSTraceSource.NewArgumentNullException("pbex");
}
if (string.IsNullOrEmpty(resourceString))
{
throw PSTraceSource.NewArgumentException("resourceString");
}
_invocationInfo = pbex.CommandInvocation;
if (_invocationInfo != null)
{
_commandName = _invocationInfo.MyCommand.Name;
}
IScriptExtent errorPosition = null;
if (_invocationInfo != null)
{
errorPosition = _invocationInfo.ScriptPosition;
}
_line = pbex.Line;
_offset = pbex.Offset;
_parameterName = pbex.ParameterName;
_parameterType = pbex.ParameterType;
_typeSpecified = pbex.TypeSpecified;
_errorId = pbex.ErrorId;
_resourceString = resourceString;
if (args != null)
{
_args = args;
}
base.SetErrorCategory(pbex.ErrorRecord._category);
base.SetErrorId(_errorId);
if (_invocationInfo != null)
{
base.ErrorRecord.SetInvocationInfo(new InvocationInfo(_invocationInfo.MyCommand, errorPosition));
}
}
#endregion Preferred constructors
#region serialization
///
/// Constructors a ParameterBindingException using serialized data.
///
///
/// serialization information
///
///
/// streaming context
///
protected ParameterBindingException(
SerializationInfo info,
StreamingContext context)
: base(info, context)
{
_message = info.GetString("ParameterBindingException_Message");
_parameterName = info.GetString("ParameterName");
_line = info.GetInt64("Line");
_offset = info.GetInt64("Offset");
}
///
/// Serializes the exception.
///
///
/// serialization information
///
///
/// streaming context
///
[SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
if (info == null)
{
throw new PSArgumentNullException("info");
}
base.GetObjectData(info, context);
info.AddValue("ParameterBindingException_Message", this.Message);
info.AddValue("ParameterName", _parameterName);
info.AddValue("Line", _line);
info.AddValue("Offset", _offset);
}
#endregion serialization
#region Do Not Use
///
/// Constructs a ParameterBindingException.
///
///
/// DO NOT USE!!!
///
public ParameterBindingException() : base() {; }
///
/// Constructors a ParameterBindingException.
///
///
/// Message to be included in exception.
///
///
/// DO NOT USE!!!
///
public ParameterBindingException(string message) : base(message) { _message = message; }
///
/// Constructs a ParameterBindingException.
///
///
/// Message to be included in the exception.
///
///
/// exception that led to this exception
///
///
/// DO NOT USE!!!
///
public ParameterBindingException(
string message,
Exception innerException)
: base(message, innerException)
{ _message = message; }
#endregion Do Not Use
#endregion Constructors
#region Properties
///
/// Gets the message for the exception.
///
public override string Message
{
get { return _message ?? (_message = BuildMessage()); }
}
private string _message;
///
/// Gets the name of the parameter that the parameter binding
/// error was encountered on.
///
public string ParameterName
{
get
{
return _parameterName;
}
}
private string _parameterName = string.Empty;
///
/// Gets the type the parameter is expecting.
///
public Type ParameterType
{
get
{
return _parameterType;
}
}
private Type _parameterType;
///
/// Gets the Type that was specified as the parameter value.
///
public Type TypeSpecified
{
get
{
return _typeSpecified;
}
}
private Type _typeSpecified;
///
/// Gets the errorId of this ParameterBindingException.
///
public string ErrorId
{
get
{
return _errorId;
}
}
private string _errorId;
///
/// Gets the line in the script at which the error occurred.
///
public Int64 Line
{
get
{
return _line;
}
}
private Int64 _line = Int64.MinValue;
///
/// Gets the offset on the line in the script at which the error occurred.
///
public Int64 Offset
{
get
{
return _offset;
}
}
private Int64 _offset = Int64.MinValue;
///
/// Gets the invocation information about the command.
///
public InvocationInfo CommandInvocation
{
get
{
return _invocationInfo;
}
}
private InvocationInfo _invocationInfo;
#endregion Properties
#region private
private string _resourceString;
private object[] _args = Array.Empty