Skip to content

Member-access enumeration works with setter methods, but not with property assignments #5271

@mklement0

Description

@mklement0

You can use member enumeration to get property values from all members of a collection, but you cannot currently set them (to a given, single value).
By contrast, you can use a setter method to do so.

# Create 2 temporary files and collect their [System.IO.FileInfo] representations
# in an array.
$files = (New-Item -Type File -Force '/tmp/t1.txt'), (New-Item -Force -Type File '/tmp/t2.txt')

# Using member enumeration, try to set a property on all elements of the array
# to the same value:

# Try to to *assign to a property*.
# !! FAILS with error "The property 'LastWriteTime' cannot be found on this object."
$files.LastWriteTime = (Get-Date).AddDays(-1)     

# Try the property's *setter method* - this works.
$files.set_LastWriteTime((Get-Date).AddDays(-1))

# Verify that the date was set to yesterday.
$files

Given that using the setter method works, I suggest also allowing the property-assignment form.
(Note that NoteProperty members, such as found on [pscustomobject] instances, do not have setter methods.)

If there are good reasons not to allow that, I suggest making the error message less confusing (if $someColl.foo finds property foo on the collection members, then $someColl.foo = ... should indicate that assignment is disallowed rather than claiming that the property doesn't exist).

Environment data

PowerShell Core v6.0.0-beta.9 on macOS 10.13
PowerShell Core v6.0.0-beta.9 on Ubuntu 16.04.3 LTS
PowerShell Core v6.0.0-beta.9 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)
Windows PowerShell v5.1.15063.674 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-By DesignThe reported behavior is by design.WG-Languageparser, language semantics

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions