Skip to content

The PSObject implementation does not handle classes derived from DynamicObject properly #6567

Description

This repro’s on 5.1 and 6. The problem is that if I have a DynamicObject that has a dynamic property “Foo”, things like $obj.Foo work, but Select-Object -ExpandProperty Foo do not.

Steps to reproduce

Add-Type @"
using System;
using System.Collections.Generic;
using System.Dynamic;

public class MyDynamicObject : DynamicObject
{
    public override IEnumerable<string> GetDynamicMemberNames()
    {
        return new string[] { "Foo" };
    }

    public override bool TryGetMember( GetMemberBinder binder, out object result )
    {
        result = null;
        bool succeeded = false;

        if( binder.Name == "Foo" )
        {
            result = 123;
            succeeded = true;
        }

        return succeeded;
    }
}
"@

$myObj = [MyDynamicObject]::new()

$myObj.Foo                                  # <-- works
$myObj | %{ $_.Foo }                        # <-- works
$myObj | Select-Object -ExpandProperty Foo  # <-- DOES NOT WORK

Expected behavior

The last three lines should all work (yield 123).

Actual behavior

The Select-Object statement throws. ("Select-Object : Property "Foo" cannot be found.")

Environment data

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      6.1.0-preview.1
PSEdition                      Core
GitCommitId                    v6.1.0-preview.1
OS                             Microsoft Windows 10.0.16299
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Activity

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 productResolution-FixedThe issue is fixed.WG-Enginecore PowerShell engine, interpreter, and runtime

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions