What's wrong with what you have? This works fine in PowerShell 5.0:
Class ControllerError : System.Exception
{
}
Class OperationFailed : ControllerError
{
$Code
$Message
OperationFailed ($Code, $Message)
{
$this.Code = $Code
$this.Message = $Message
}
}
$o = [operationfailed]::new("500","Internal server errror")
$o.Code
500
$o.Message
Internal server errror
$o | Get-Member | Select Name, MemberType
Name MemberType
---- ----------
Equals Method
GetBaseException Method
GetHashCode Method
GetObjectData Method
GetType Method
ToString Method
Code Property
Data Property
HelpLink Property
HResult Property
InnerException Property
Message Property
Source Property
StackTrace Property
TargetSite Property
$o.gettype()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True False OperationFailed ControllerError
$o.gettype().BaseType
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True False ControllerError System.Exception
I would recommend specifying a type for $Code
and $Message
as you probably don't want them to be System.Object
. Ex.:
Class OperationFailed : ControllerError
{
[string]$Code
[string]$Message
OperationFailed ([string]$Code, [string]$Message)
{
$this.Code = $Code
$this.Message = $Message
}
}
If you really want to use the base constructor (or you want to use Powershell < 5.0), then you need to use C# and Add-Type
$def = @"
public class ControllerError : System.Exception
{
public ControllerError(string message) : base(message) { }
}
public class OperationFailed : ControllerError
{
public string Code;
public OperationFailed(string code, string message) : base(message)
{
this.Code = code;
}
}
"@
Add-Type -TypeDefinition $def
$o = [OperationFailed]::new("500","internal server error")