/********************************************************************++ Copyright (c) Microsoft Corporation. All rights reserved. --********************************************************************/ using System.Runtime.Serialization; #if !CORECLR using System.Security.Permissions; #else // Use stub for SerializableAttribute. using Microsoft.PowerShell.CoreClr.Stubs; #endif namespace System.Management.Automation { /// /// This is a wrapper for exception class /// /// which provides additional information via /// . /// /// /// Instances of this exception class are usually generated by the /// Monad Engine. It is unusual for code outside the Monad Engine /// to create an instance of this class. /// [Serializable] public class PSArgumentNullException : ArgumentNullException, IContainsErrorRecord { #region ctor /// /// Initializes a new instance of the PSArgumentNullException class. /// /// constructed object public PSArgumentNullException() : base() { } /// /// Initializes a new instance of the PSArgumentNullException class. /// /// /// constructed object /// /// Per MSDN, the parameter is paramName and not message. /// I confirm this experimentally as well. /// public PSArgumentNullException(string paramName) : base(paramName) { } /// /// Initializes a new instance of the PSArgumentNullException class. /// /// /// /// constructed object public PSArgumentNullException(string message, Exception innerException) : base(message, innerException) { _message = message; } /// /// Initializes a new instance of the PSArgumentNullException class. /// /// /// /// constructed object /// /// ArgumentNullException has this ctor form and we imitate it here. /// public PSArgumentNullException(string paramName, string message) : base(paramName, message) { _message = message; } #region Serialization #if !CORECLR // ArgumentNullException Has No Serialization In CoreCLR /// /// Initializes a new instance of the PSArgumentNullException class /// using data serialized via /// /// /// serialization information /// streaming context /// constructed object protected PSArgumentNullException(SerializationInfo info, StreamingContext context) : base(info, context) { _errorId = info.GetString("ErrorId"); _message = info.GetString("PSArgumentNullException_MessageOverride"); } /// /// Serializer for /// /// 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("ErrorId", _errorId); info.AddValue("PSArgumentNullException_MessageOverride", _message); } #endif #endregion Serialization #endregion ctor /// /// Additional information about the error /// /// /// /// Note that ErrorRecord.Exception is /// . /// public ErrorRecord ErrorRecord { get { if (null == _errorRecord) { _errorRecord = new ErrorRecord( new ParentContainsErrorRecordException(this), _errorId, ErrorCategory.InvalidArgument, null); } return _errorRecord; } } private ErrorRecord _errorRecord; private string _errorId = "ArgumentNull"; /// /// see /// /// /// Exception.Message is get-only, but you can effectively /// set it in a subclass by overriding this virtual property. /// /// public override string Message { get { return String.IsNullOrEmpty(_message) ? base.Message : _message; } } private string _message; } // PSArgumentNullException } // System.Management.Automation