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
Conversion of [datetime] property in class does not work #20401
Comments
I found a workaround for the problem by implementing the explicit operator:
Now it works as expected. |
It's odd - if you remove |
In the meantime I found out that it works with a
|
From the call stack it may be that this line of code is the one responsible for the error message:
If I use a |
Let me try to summarize:
The following repro code demonstrates this: class Statistics1 { [nullable[datetime]] $LastDownload } # nullable
class Statistics2 { [datetime] $LastDownload } # non-nullable
[pscustomobject] @{
'Nullable, pscustomobject' = $(try { [Statistics1] [pscustomobject] @{ LastDownLoad = Get-Date } } catch { "ERROR: $_" }) | Out-String
'Nullable, pscustomobject, no psobject wrapper' = $(try { [Statistics1] [pscustomobject] @{ LastDownLoad = [datetime]::Now } } catch { "ERROR: $_" }) | Out-String
'Nullable, hashtable' = [Statistics1] @{ LastDownLoad = Get-Date } | Out-String
'Non-nullable, pscustomobject' = [Statistics2] [pscustomobject] @{ LastDownLoad = Get-Date } | Out-String
'Non-nullable, pscustomobject, no psobject wrapper' = [Statistics2] [pscustomobject] @{ LastDownLoad = [datetime]::Now } | Out-String
'Non-nullable, hashtable' = [Statistics2] @{ LastDownLoad = Get-Date } | Out-String
} | Format-List Output: Nullable, pscustomobject : ERROR: Cannot convert value "@{LastDownLoad=9/29/2023 11:25:34 AM}" to type "Statistics1". Error: "Cannot convert value
"9/29/2023 11:25:34 AM" to type "System.Nullable`1[System.DateTime]". Error: "Cannot process argument because the value of
argument "obj" is null. Change the value of argument "obj" to a non-null value.""
Nullable, pscustomobject, no psobject wrapper :
LastDownload
------------
9/29/2023 11:25:34 AM
Nullable, hashtable :
LastDownload
------------
9/29/2023 11:25:34 AM
Non-nullable, pscustomobject :
LastDownload
------------
1/1/0001 12:00:00 AM
Non-nullable, pscustomobject, no psobject wrapper :
LastDownload
------------
9/29/2023 11:25:34 AM
Non-nullable, hashtable :
LastDownload
------------
9/29/2023 11:25:34 AM Note how the use of non- Conceptually related (in that initialization by |
The Engine WG discussed this and agree it's a bug And also that most likely the cause of the bug is that the conversion path isn't handling when |
Resolved and closed by "no activity". OK, it is not such a big bug... |
Prerequisites
Steps to reproduce
I implemented this class in file "statistics.ps1" and want to use the automatic cast/conversion utilities of PowerShell:
Expected behavior
Actual behavior
Error details
Environment data
Visuals
No response
The text was updated successfully, but these errors were encountered: