mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Fix PowerShell class to support deriving from an abstract class with abstract properties (#21331)
This commit is contained in:
@@ -628,3 +628,47 @@ class Derived : Base
|
||||
$sb.Invoke() | Should -Be 200
|
||||
}
|
||||
}
|
||||
|
||||
Describe 'Base type has abstract properties' -Tags "CI" {
|
||||
It 'can derive from `FileSystemInfo`' {
|
||||
## FileSystemInfo has 3 abstract members that a derived type needs to implement
|
||||
## - public abstract bool Exists { get; }
|
||||
## - public abstract string Name { get; }
|
||||
## - public abstract void Delete ();
|
||||
|
||||
class myFileSystemInfo : System.IO.FileSystemInfo
|
||||
{
|
||||
[string] $Name
|
||||
[bool] $Exists
|
||||
|
||||
myFileSystemInfo([string]$path)
|
||||
{
|
||||
# ctor
|
||||
$this.Name = $path
|
||||
$this.Exists = $true
|
||||
}
|
||||
|
||||
[void] Delete()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
$myFile = [myFileSystemInfo]::new('Hello')
|
||||
$myFile.Name | Should -Be 'Hello'
|
||||
$myFile.Exists | Should -BeTrue
|
||||
}
|
||||
|
||||
It 'deriving from `FileSystemInfo` will fail when the abstract property `Exists` is not implemented' {
|
||||
$script = [scriptblock]::Create('class WillFail : System.IO.FileSystemInfo { [string] $Name }')
|
||||
$failure = $null
|
||||
try {
|
||||
& $script
|
||||
} catch {
|
||||
$failure = $_
|
||||
}
|
||||
|
||||
$failure | Should -Not -BeNullOrEmpty
|
||||
$failure.FullyQualifiedErrorId | Should -BeExactly "TypeCreationError"
|
||||
$failure.Exception.Message | Should -BeLike "*'get_Exists'*"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user