Skip to content

PowerShell does not property overrides that only override the set #25038

Open

Description

@vcsjones

Prerequisites

Steps to reproduce

In C#, it is possible for a property to override only the set for a property that has both a get and a set. However, PowerShell will believe that the property only has a set.

Simple steps to reproduce:

$Source = @"
public class MyBase
{
    protected int _foo;

    public virtual int Foo {
        get => _foo;
        set => _foo = value;
    }
}

public class BlahBlah : MyBase {
    public override int Foo {
        set => _foo = value + 1;
    }
}
"@

Add-Type -TypeDefinition $Source
$b = New-Object -TypeName BlahBlah
$f = $b.Foo

Expected behavior

I would expect the above snippet to assign the value of `0` to `$f`

Actual behavior

Raises the error

> Cannot get property value because "Foo" is a write-only property.

Error details

Exception             :
    Type        : System.Management.Automation.RuntimeException
    ErrorRecord :
        Exception             :
            Type    : System.Management.Automation.ParentContainsErrorRecordException
            Message : Cannot get property value because "Foo" is a write-only property.
            HResult : -2146233087
        CategoryInfo          : InvalidOperation: (:) [], ParentContainsErrorRecordException
        FullyQualifiedErrorId : WriteOnlyProperty
        InvocationInfo        :
            ScriptLineNumber : 1
            OffsetInLine     : 1
            HistoryId        : 6
            Line             : $f = $b.Foo
            Statement        : $f = $b.Foo
            PositionMessage  : At line:1 char:1
                               + $f = $b.Foo
                               + ~~~~~~~~~~~
            CommandOrigin    : Internal
        ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1
    TargetSite  : System.Object CallSite.Target(System.Runtime.CompilerServices.Closure,
System.Runtime.CompilerServices.CallSite, System.Object)
    Message     : Cannot get property value because "Foo" is a write-only property.
    Data        : System.Collections.ListDictionaryInternal
    Source      : Anonymously Hosted DynamicMethods Assembly
    HResult     : -2146233087
    StackTrace  :
   at CallSite.Target(Closure, CallSite, Object)
   at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
   at System.Management.Automation.Interpreter.DynamicInstruction`2.Run(InterpretedFrame
frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(Interpret
edFrame frame)
CategoryInfo          : InvalidOperation: (:) [], RuntimeException
FullyQualifiedErrorId : WriteOnlyProperty
InvocationInfo        :
    ScriptLineNumber : 1
    OffsetInLine     : 1
    HistoryId        : 6
    Line             : $f = $b.Foo
    Statement        : $f = $b.Foo
    PositionMessage  : At line:1 char:1
                       + $f = $b.Foo
                       + ~~~~~~~~~~~
    CommandOrigin    : Internal
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1

Environment data

Name                           Value
----                           -----
PSVersion                      7.5.0
PSEdition                      Core
GitCommitId                    7.5.0
OS                             Darwin 24.3.0 Darwin Kernel Version 24.3.0: Thu Jan  2 20:24…
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

Activity

  1. vcsjones commented on Feb 17, 2025

    @vcsjones
    Author

    To add, this was originally reported to .NET because there are some classes that have this shape. Some classes only override the set of a property (to have additional validation, for example)

  2. vcsjones commented on Feb 18, 2025

    @vcsjones
    Author

    Further adding - this can be reproduced on PowerShell 7.5 using the following APIs built-in to .NET:

    $rsa = New-Object System.Security.Cryptography.RSAOpenSsl
    $s = rsa.KeySize

    (Note this particular example is Linux only)

  3. SeeminglyScience commented on Feb 25, 2025

    @SeeminglyScience
    Contributor

    The Engine WG discussed this and agree it's a bug. Not marking as up for grabs only because this would be a particularly challenging issue to take on if you are not already familiar with the systems involved. Those who are sufficiently familiar are welcome to open a PR

  4. microsoft-github-policy-service commented on Aug 24, 2025

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if there is no further activity in 7 days, the issue will be closed automatically.

    Activity in this case refers only to comments on the issue. If the issue is closed and you are the author, you can re-open the issue using the button below. Please add more information to be considered during retriage. If you are not the author but the issue is impacting you after it has been closed, please submit a new issue with updated details and a link to this issue and the original.

  5. added
    KeepOpenThe bot will ignore these and not auto-close
    and removed
    Resolution-No ActivityIssue has had no activity for 6 months or more
    on Aug 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-BugIssue has been identified as a bug in the productKeepOpenThe bot will ignore these and not auto-closeWG-Enginecore PowerShell engine, interpreter, and runtimeWG-ReviewedA Working Group has reviewed this and made a recommendation

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions