Skip to content

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

@vcsjones

Description

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

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