New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Simplified syntax doesn't recognize static members. #21514
Comments
While I agree the documentation in the about page and for the Add-Type @'
public class Foo
{
public static string Bar(string s) { return s; }
}
'@
$f = [Foo]::new()
$f.Bar('a') # Doesn't work
$f | Get-Member # Doesn't show Bar
$f | Get-Member -Static # Does show Bar |
Therefore, there are two possible resolutions:
|
Personally it makes sense for ForEach-Object to run on multiple instances in an array but not for a static method. Why would you be enumerating multiple instances to call a static method that has no correlation to that instance itself. While I don't know what the implementation of this is it sounds like it could add more overhead as now it needs to query both the ETS members and then the members on the type itself which isn't part of the object but the .NET type itself. Ultimately a question for the WG. |
You're right: I've been trying to come with real-world examples of when static member access may be useful with simplified syntax, and so far the only - not very compelling - examples I've come up with are the following: # Possibly should be the same as:
# [regex] 'foo' | % { $_.GetType()::CacheSize }
[regex] 'foo' | % CacheSize
# Possibly should be the same as:
# [regex] 'foo' | % { $_.GetType()::unescape('a\\b') }
[regex] 'foo' | % Unescape 'a\\b' To put it differently: the suggested ability to access static members would only be useful for static properties and for static methods whose parameters are (by definition) unrelated to the specific properties of each input instance. Therefore - unless someone finds additional angles - I agree that amending the docs is sufficient. |
While not using the simplified syntax it could be done with with reflection.
|
@rhubarb-geek-nz: no need for reflection: (Add-Type @'
public class Foo
{
public static string Bar(string s) { return s; }
}
'@ -PassThru)::new() |
ForEach-Object { $_.GetType()::Bar('1') } |
Even just |
I appreciate all the feedback. I've concluded that adding a note to the docs is sufficient, so I'm closing this. See: |
📣 Hey @mklement0, how did we do? We would love to hear your feedback with the link below! 🗣️ 🔗 https://aka.ms/PSRepoFeedback |
Prerequisites
Steps to reproduce
Note:
Expected behavior
1
That is, the static
.Bar()
method should be called.Actual behavior
The following error occurs:
Input name "Bar" cannot be resolved to a method.
Error details
No response
Environment data
Visuals
No response
The text was updated successfully, but these errors were encountered: