-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Fix PowerShell class to support deriving from an abstract class with abstract properties #21331
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -628,3 +628,47 @@ class Derived : Base | |
| $sb.Invoke() | Should -Be 200 | ||
| } | ||
| } | ||
|
|
||
| Describe 'Base type has abstract properties' -Tags "CI" { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we have validation which ensures that an error is produced if the abstract property is not defined? is it needed?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point. I will add one.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A test for the error case was added. |
||
| 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'*" | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this will need to include
BindingFlags.NonPublicasprotectedmembers should still be found right? LikewiseGetAccessorsbelow likely needsnonPublic: true.Then a check like
Interfaces may also actually need that check too since C# 8 added the ability to mark members as protected. Though that wouldn't be fixing a regression so likely fine to leave out of this PR.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But we don't create properties or methods with the "protected" (
family) modifier, and we don't have a keyword in PowerShell language to declare a property asprotected, we only createpublic(by default or with thehiddenkeyword) orstatic(statickeyword) members. So, even if we check forprotected abstractproperties here, thepublicproperty we are going to create later won't be considered as an implementation of theprotectedproperty.When a base type (
MyBase) has a protected abstract propertyName, the class definitionclass MySub : MyBase { [string]$Name }will fail on 7.2 and 7.3 as well today (see the screenshots below). So, I think we just don't support deriving from a class that has protected abstract properties/methods.7.2.18

7.3.11

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah alright if it failed before then good enough for me!