Skip to content

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

Closed

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"
    }
}

Activity

  1. vexx32 commented on Sep 7, 2018

    @vexx32
    Collaborator

    That probably shouldn't be allowed... I think?

    But if this arises, you should be able to access the property with $Object.PSObject.Properties.Where{$_.Name -eq 'PropertyName'}.Value

    Also, any particular reason you're not using [System.IO.DirectoryInfo] instead of a custom class, just out of curiosity? 😄

  2. iSazonov commented on Sep 10, 2018

    @iSazonov
    Collaborator

    Stéphane (@Stephanevg) I don't see "Steps to reproduce".

  3. Stephanevg commented on Sep 10, 2018

    @Stephanevg
    Author

    Ilya (@iSazonov) sorry for that. I updated my original post.
    But, as Rain Sallow (/u/ta11ow) (@vexx32) mentionned, it is perhaps a best practise not to have a method named as a property.
    , It could be that it is then kind of a security mechanism, where PS simply removes the property.

    But if that is the case, we should have at least some guidance somewhere (PSSCRiptAnalyzer?), or simply throw an error perhaps? as without anything, mentioning that it disapears clearly felt strange to me.

  4. iSazonov commented on Sep 10, 2018

    @iSazonov
    Collaborator

    Stéphane (@Stephanevg) Thanks for the update.

    Seems we already have a similar registered Issue. (Please search.)
    We have plans to enhance class support in PowerShell Core and I believe the issue could be fixed too.

  5. added
    Issue-Bug
    WG-Engine
    on Sep 10, 2018
  6. vexx32 commented on Sep 10, 2018

    @vexx32
    Collaborator

    FWIW this will also occur with other similar situations. For example, adding a key named "keys" to a hashtable will prevent listing the keys unless you go into .PSObject.Base.Keys or some such.

    It's a generic issue with the binder, I'd say, honestly; there needs to be a robust method of specifying a property in cases like these.

  7. iSazonov commented on Sep 11, 2018

    @iSazonov
    Collaborator

    Rob Holt (@rjmholt) I think make sense add the Issue in your check list for classes.

  8. rjmholt commented on Sep 11, 2018

    @rjmholt
    Collaborator

    Rain Sallow (/u/ta11ow) (@vexx32) That hashtable example sounds not good. I would open a separate issue for that one and tag the @PowerShell/powershell-committee

  9. rjmholt commented on Sep 11, 2018

    @rjmholt
    Collaborator

    (The issue may be generic, but fixing it once for everything sounds like a breaking change and I can just imagine needing to twist around it in 9 ways)

  10. vexx32 commented on Sep 11, 2018

    @vexx32
    Collaborator

    That hashtable example is not good, and very difficult to work around! I'll open another one.

  11. SteveL-MSFT commented on Sep 11, 2018

    @SteveL-MSFT
    Member

    C# doesn't allow this, we should error

  12. Stephanevg commented on Sep 21, 2018

    @Stephanevg
    Author

    Rain Sallow (/u/ta11ow) (@vexx32) I am not, this was just for an example case. I faced this issue in a more complex class, and wanted to narrow it down to the essentials of the problem ;)

    I don't think we actually need to allow this behaviour. If this is a bad practice, why allow it? Totally ok with have more strict rules in the naming of the properties / methods of our classes.

    I think that writing an error + mentioning it in the docs - somehwere - that it isn't allowed, would be sufficent, I guess.
    Having it just disapear like that is just odd.

  13. vexx32 commented on Sep 21, 2018

    @vexx32
    Collaborator

    Yeah this shouldn't be allowed. Not a very good idea, haha!

  14. microsoft-github-policy-service commented on Nov 16, 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.

  15. microsoft-github-policy-service commented on Nov 16, 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.

  16. microsoft-github-policy-service commented on Nov 16, 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.

  17. added
    Resolution-No Activity
    on Nov 16, 2023
  18. microsoft-github-policy-service commented on Nov 23, 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-BugResolution-No ActivityWG-Engine

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions