Skip to content

When powershell class method has same name as property, the property disapear and is not accessible from any instance #7736

@Stephanevg

Description

@Stephanevg

Hi,

When I have a powershell class, that has a method with the same name as one of it's properties, then the property disapear, and is not visible on any of the instances.

See examples below.

Steps to reproduce

Class Folder {
    
    $Name
    $Path
    [bool]$IsPresent

    [string]IsPresent(){
        
        return "woop"
    }
}

When looking using Get-Member, we can see that the property $IsPresent disapeared, but that method is still there:

[Folder]::New() | gm


   TypeName: Folder

Name        MemberType Definition                    
----        ---------- ----------                    
Equals      Method     bool Equals(System.Object obj)
GetHashCode Method     int GetHashCode()             
GetType     Method     type GetType()                
IsPresent   Method     string IsPresent()            
ToString    Method     string ToString()             
Name        Property   System.Object Name {get;set;} 
Path        Property   System.Object Path {get;set;

Expected behavior

[Folder]::New()

#Should return

Name Path IsPresent
---- ---- ---------
              False

Actual behavior

[Folder]::New()

#Returns

Name Path
---- ----

Environment data

I had the same behaviour on:

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

and

Name                           Value                                                                                            
----                           -----                                                                                            
PSVersion                      5.1.14393.2339                                                                                   
PSEdition                      Desktop                                                                                          
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                          
BuildVersion                   10.0.14393.2339                                                                                  
CLRVersion                     4.0.30319.42000                                                                                  
WSManStackVersion              3.0                                                                                              
PSRemotingProtocolVersion      2.3                                                                                              
SerializationVersion           1.1.0.1                                                                                          

On Windows 10 and Windows Server 2012 R2

Workaround

The workaround would be to change the name of the method and ensure it is not identical to the property anymore:

Class Folder {
    
    $Name
    $Path
    [bool]$IsPresent

    [string]TestIsPresent(){
        
        return "woop"
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-BugIssue has been identified as a bug in the productResolution-No ActivityIssue has had no activity for 6 months or moreWG-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