Skip to content

Class with hidden property breaking for-display formatting? #17071

Closed

Description

Prerequisites

Steps to reproduce

Object instances with hidden or private properties could be breaking the for-display formatting when piped to Get-Member as well as Select-Object *.

class MyBool2 {
    hidden [bool] $Value
    
    MyBool2([bool] $Value) {
        $this.Value = $Value
    }

    [string] ToString() {
        return "$($this.Value)"
    }
}

Expected behavior

PS /> $instance = [MyBool2] $true
PS /> $instance
True

PS /> $instance | Get-Member

   TypeName: MyBool2

Name        MemberType Definition
----        ---------- ----------
Equals      Method     bool Equals(System.Object obj)
GetHashCode Method     int GetHashCode()
GetType     Method     type GetType()
ToString    Method     string ToString()

PS /> $instance
True

PS /> $instance.ToString()
True

Actual behavior

PS /> $instance = [MyBool2] $true
PS /> $instance
True

PS /> $instance | Get-Member

   TypeName: MyBool2

Name        MemberType Definition
----        ---------- ----------
Equals      Method     bool Equals(System.Object obj)
GetHashCode Method     int GetHashCode()
GetType     Method     type GetType()
ToString    Method     string ToString()

PS /> $instance


PS /> $instance.ToString()
True

Error details

No errors.

Environment data

Name                           Value
----                           -----
PSVersion                      7.2.2
PSEdition                      Core
GitCommitId                    7.2.2
OS                             Microsoft Windows 10.0.19043
Platform                       Win32NT
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. added
    Needs-Triage
    on Mar 27, 2022
  2. added
    Issue-Bug
    on Mar 27, 2022
  3. zett42 commented on Mar 27, 2022

    @zett42

    It might be hard to see, the output of $instance after the call to Get-Member only consists of a newline instead of the expected True.

  4. kasini3000 commented on Mar 30, 2022

    @kasini3000

    Seems this a vague question,
    Actual behavior same with Expected behavior

  5. zett42 commented on Mar 30, 2022

    @zett42

    Actual behavior same with Expected behavior

    It isn't. Compare the output of $instance after call to Get-Member.

    Expected:

    PS /> $instance | Get-Member
    ...
    PS /> $instance
    True

    Actual:

    PS /> $instance | Get-Member
    ...
    PS /> $instance
    

    Note that there is just an empty line in the actual output of $instance.

    When inserting a [Console]::WriteLine() call in the ToString() method, we can observe that ToString() isn't even called anymore. It appears that Get-Member corrupts the object in a way that its default output no longer works.

    The corruption only occurs for the object that gets passed to Get-Member. When creating a new instance of MyBool2 afterwards, it's output is correct again.

    More observations:

    $instance | Get-Member
    "$instance"                   # works
    $instance | Out-String  # doesn't work

    Conclusion: Any output that involves PowerShell's formatting system appears to be broken.

  6. kasini3000 commented on Mar 31, 2022

    @kasini3000

    I understood the problem from your explain, and found inconsistent behavior.

    class MyBool2
    {
    	hidden [bool] $Value
    
    	MyBool2([bool] $V)
    	{
    		$this.Value = $V
    	}
    
    	[string] ToString()
    	{
    		if ($this.Value -eq $true)
    		{
    			return 'true'
    		}
    		else
    		{
    			return 'false'
    		}
    	}
    }
    '-----1----'
    $instance = [mybool2] $true
    $instance
    '-----2----'
    $instance  | Get-Member
    $instance
    '-----3----'
    "$instance"
    

    ps 5.1,ps 6.2.7:

    -----1----
    true
    -----2----
    
    
       TypeName: MyBool2
    Name        MemberType Definition
    ----        ---------- ----------
    Equals      Method     bool Equals(System.Object obj)
    GetHashCode Method     int GetHashCode()
    GetType     Method     type GetType()
    ToString    Method     string ToString()
    true
    -----3----
    true
    

    ps 7.3.3

    -----1----
    true
    -----2----
    
       TypeName: MyBool2
    
    Name        MemberType Definition
    ----        ---------- ----------
    Equals      Method     bool Equals(System.Object obj)
    GetHashCode Method     int GetHashCode()
    GetType     Method     type GetType()
    ToString    Method     string ToString()
    
    -----3----
    true
    
    
  7. microsoft-github-policy-service commented on Nov 15, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  8. added
    Resolution-No Activity
    on Nov 15, 2023
  9. microsoft-github-policy-service commented on Nov 22, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

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-BugNeeds-TriageResolution-No ActivityWG-Cmdlets

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions