Repository navigation
Fix getting a property whose override defines only the setter - #28282
Open
Caner Demir (piupiunn) wants to merge 3 commits into
Open
Caner Demir (piupiunn) wants to merge 3 commits into
Caner Demir (piupiunn) wants to merge 3 commits into
Conversation
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The change regresses indexer setters’ optional-argument defaults and params support, and breaks protected-property assignments involving generic base types.
2 open issues
What changed in this PR
Addresses #25038 by allowing PowerShell to read inherited getters when a .NET property override defines only the setter.
Changes:
- Resolves inherited getters when populating the .NET property cache.
- Applies the same lookup to non-public properties accessed inside PowerShell classes.
- Adds regression tests for ordinary, indexed, generic-base, and protected properties, including virtual dispatch.
| File | Description |
|---|---|
test/powershell/engine/ETS/Adapter.Tests.ps1 |
Adds setter-only override regression tests. |
src/System.Management.Automation/engine/runtime/Binding/Binders.cs |
Uses inherited-getter lookup for non-public property binding. |
src/System.Management.Automation/engine/CoreAdapter.cs |
Adds inherited-getter resolution and integrates it into property caching. |
Review effort: Balanced
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

PR Summary
PowerShell can now get a .NET property whose override defines only the setter. The getter is inherited from the base property, as in C#.
Fix #25038
PR Context
For
public override int Foo { set { ... } }, reflection returns aPropertyInfofor the override that has only the setter, although the getter is inherited. The .NET adapter used thatPropertyInfofor getting too, so the property was treated as write-only:$obj.FoothrewWriteOnlyProperty.$obj.psobject.Properties['Foo'].ValueandSelect-Object Fooreturned$null.Get-Membershowedint Foo {set;}.PropertyCacheEntrystill stores the override'sPropertyInfo, so setting, attributes and the declaring type don't change. Only for getting, when the override has no getter, it now uses the property that introduced the virtual property (found withMethodInfo.GetBaseDefinition()). That getter is virtual, so a getter override in a type in between is still called.ParameterizedPropertyCacheEntrytakes the inherited getter the same way, so$obj.Item(1)works too, and the setter keeps the override's parameters. Protected properties used inside a PowerShell class are covered as well, because they also get aPropertyCacheEntry.Other visible changes for these properties:
$obj.Item(1)now calls the matching overload instead of another overload that has a getter,Get-Membershows an indexer as the base getter declares it, andSelect-Object *,Format-List *andConvertTo-Jsonnow output their values.In-box properties that become readable:
XmlDocument.InnerText,XmlAttribute.InnerTextandXmlAttribute.InnerXml. For example,([xml]'<a>b</a>').InnerTextreturns$nulltoday andbwith this change.AnonymousPipeServerStream.ReadModeandAnonymousPipeClientStream.ReadMode.RSAOpenSsl.KeySizeon Linux (### **Title:** MissingKeySizeReading Access inRSACertificateExtensions.GetRSAPrivateKeyon Linux (.NET 9) dotnet/runtime#112622).SiteofErrorProvider,AxHostandWebBrowserBase;HwndTarget.RootVisual.Not changed:
public override int Foo { protected set { ... } }, used outside a PowerShell class that derives from the type. Reflection doesn't return such a property as public, so it isn't in the property table.{ set; }, becauseCompletionCompletersreads the override'sPropertyInfoitself.RSACryptoServiceProvider.KeySize), although C# can call the inherited setter. I left it out because the issue doesn't cover it, and it would make some properties settable that look read-only today.I can handle the last two here or in separate PRs if you want them.
The new tests fail without the fix and pass with it.
PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright header