Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Conversion of [datetime] property in class does not work #20401

Copy link
Copy link
@ghost

Description

Prerequisites

Steps to reproduce

I implemented this class in file "statistics.ps1" and want to use the automatic cast/conversion utilities of PowerShell:

class Statistics {
	[ValidateNotNullOrEmpty()]
	[nullable[datetime]] $LastDownload

	[ValidateRange("Positive")]
	[int] $Downloads

	[ValidateNotNull()]
	[string] $DownloadedBy = "n/a"
}

Expected behavior

PS> . .\statistics.ps1
PS> $object = [pscustomobject]@{ LastDownload = (Get-Date); Downloads = 123; DownloadedBy = "Jesus!" }
PS> [StatisticsExp]$object

LastDownload        Downloads DownloadedBy
------------        --------- ------------
29.09.2023 15:04:40       123 Jesus!

Actual behavior

PS> . .\statistics.ps1
PS> $object = [pscustomobject]@{ LastDownload = (Get-Date); Downloads = 123; DownloadedBy = "Jesus!" }
PS> [StatisticsExp]$object
InvalidArgument: Cannot convert value "@{LastDownload=29.09.2023 15:04:40; Downloads=123; DownloadedBy=Jesus!}" to type "Statistics". Error: "Cannot convert value "29.09.2023 15:04:40" 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.""

Error details

PS> Get-Error

Exception             : 
    Type           : System.Management.Automation.RuntimeException
    ErrorRecord    : 
        Exception             : 
            Type    : System.Management.Automation.ParentContainsErrorRecordException
            Message : Cannot convert value "@{LastDownload=29.09.2023 15:04:40; Downloads=123; DownloadedBy=Jesus!}" to type "Statistics". Error: "Cannot convert value "29.09.2023 15:04:40" 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.""
            HResult : -2146233087
        CategoryInfo          : InvalidArgument: (:) [], ParentContainsErrorRecordException
        FullyQualifiedErrorId : InvalidCastConstructorException
        InvocationInfo        : 
            ScriptLineNumber : 1
            OffsetInLine     : 1
            HistoryId        : -1
            Line             : [Statistics]$object
            PositionMessage  : At line:1 char:1
                               + [Statistics]$object
                               + ~~~~~~~~~~~~~~~~~~~
            CommandOrigin    : Internal
        ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1
    Message        : Cannot convert value "@{LastDownload=29.09.2023 15:04:40; Downloads=123; DownloadedBy=Jesus!}" to type "Statistics". Error: "Cannot convert value "29.09.2023 15:04:40" 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.""
    InnerException : 
        Type           : System.Management.Automation.PSInvalidCastException
        ErrorRecord    : 
            Exception             : 
                Type    : System.Management.Automation.ParentContainsErrorRecordException
                Message : Cannot convert value "@{LastDownload=29.09.2023 15:04:40; Downloads=123; DownloadedBy=Jesus!}" to type "Statistics". Error: "Cannot convert value "29.09.2023 15:04:40" 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.""
                HResult : -2146233087
            CategoryInfo          : InvalidArgument: (:) [], ParentContainsErrorRecordException
            FullyQualifiedErrorId : InvalidCastConstructorException
            InvocationInfo        : 
                ScriptLineNumber : 1
                OffsetInLine     : 1
                HistoryId        : -1
                Line             : [Statistics]$object
                PositionMessage  : At line:1 char:1
                                   + [Statistics]$object
                                   + ~~~~~~~~~~~~~~~~~~~
                CommandOrigin    : Internal
            ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1
        TargetSite     : 
            Name          : Convert
            DeclaringType : System.Management.Automation.LanguagePrimitives+ConvertViaNoArgumentConstructor, System.Management.Automation, Version=7.3.7.500, Culture=neutral, PublicKeyToken=31bf3856ad364e35
            MemberType    : Method
            Module        : System.Management.Automation.dll
        Message        : Cannot convert value "@{LastDownload=29.09.2023 15:04:40; Downloads=123; DownloadedBy=Jesus!}" to type "Statistics". Error: "Cannot convert value "29.09.2023 15:04:40" 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.""
        Data           : System.Collections.ListDictionaryInternal
        InnerException : 
            Type           : System.Management.Automation.PSInvalidCastException
            ErrorRecord    : 
                Exception             : 
                    Type    : System.Management.Automation.ParentContainsErrorRecordException
                    Message : Cannot convert value "29.09.2023 15:04:40" 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."
                    HResult : -2146233087
                CategoryInfo          : InvalidArgument: (:) [], ParentContainsErrorRecordException
                FullyQualifiedErrorId : InvalidCastConstructorException
            TargetSite     : 
                Name          : Convert
                DeclaringType : System.Management.Automation.LanguagePrimitives+ConvertViaNoArgumentConstructor, System.Management.Automation, Version=7.3.7.500, Culture=neutral, PublicKeyToken=31bf3856ad364e35
                MemberType    : Method
                Module        : System.Management.Automation.dll
            Message        : Cannot convert value "29.09.2023 15:04:40" 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."
            InnerException : 
                Type        : System.Management.Automation.PSArgumentNullException
                ErrorRecord : 
                    Exception             : 
                        Type    : System.Management.Automation.ParentContainsErrorRecordException
                        Message : Cannot process argument because the value of argument "obj" is null. Change the value of argument "obj" to a non-null value.
                        HResult : -2146233087
                    CategoryInfo          : InvalidArgument: (:) [], ParentContainsErrorRecordException
                    FullyQualifiedErrorId : ArgumentNull
                Message     : Cannot process argument because the value of argument "obj" is null. Change the value of argument "obj" to a non-null value.
                ParamName   : obj
                TargetSite  : 
                    Name          : AsPSObject
                    DeclaringType : psobject
                    MemberType    : Method
                    Module        : System.Management.Automation.dll
                Source      : System.Management.Automation
                HResult     : -2147467261
                StackTrace  : 
   at System.Management.Automation.PSObject.AsPSObject(Object obj, Boolean storeTypeNameAndInstanceMembersLocally)
   at System.Management.Automation.LanguagePrimitives.SetObjectProperties(Object o, IDictionary properties, Type resultType, MemberNotFoundError memberNotFoundErrorAction, MemberSetValueError memberSetValueErrorAction, Boolean enableMethodCall, IFormatProvider
formatProvider, Boolean recursion, Boolean ignoreUnknownMembers)
   at System.Management.Automation.LanguagePrimitives.ConvertViaNoArgumentConstructor.Convert(Object valueToConvert, Type resultType, Boolean recursion, PSObject originalValueToConvert, IFormatProvider formatProvider, TypeTable backupTable, Boolean ignoreUnknownMembers) 
            Source         : System.Management.Automation
            HResult        : -2147467262
            StackTrace     : 
   at System.Management.Automation.LanguagePrimitives.ConvertViaNoArgumentConstructor.Convert(Object valueToConvert, Type resultType, Boolean recursion, PSObject originalValueToConvert, IFormatProvider formatProvider, TypeTable backupTable, Boolean ignoreUnknownMembers) 
   at System.Management.Automation.LanguagePrimitives.SetObjectProperties(Object o, IDictionary properties, Type resultType, MemberNotFoundError memberNotFoundErrorAction, MemberSetValueError memberSetValueErrorAction, Boolean enableMethodCall, IFormatProvider
formatProvider, Boolean recursion, Boolean ignoreUnknownMembers)
   at System.Management.Automation.LanguagePrimitives.SetObjectProperties(Object o, PSObject psObject, Type resultType, MemberNotFoundError memberNotFoundErrorAction, MemberSetValueError memberSetValueErrorAction, IFormatProvider formatProvider, Boolean recursion,       
Boolean ignoreUnknownMembers)
   at System.Management.Automation.LanguagePrimitives.ConvertViaNoArgumentConstructor.Convert(Object valueToConvert, Type resultType, Boolean recursion, PSObject originalValueToConvert, IFormatProvider formatProvider, TypeTable backupTable, Boolean ignoreUnknownMembers) 
        Source         : System.Management.Automation
        HResult        : -2147467262
        StackTrace     : 
   at System.Management.Automation.LanguagePrimitives.ConvertViaNoArgumentConstructor.Convert(Object valueToConvert, Type resultType, Boolean recursion, PSObject originalValueToConvert, IFormatProvider formatProvider, TypeTable backupTable, Boolean ignoreUnknownMembers) 
   at System.Management.Automation.LanguagePrimitives.ConvertViaNoArgumentConstructor.Convert(Object valueToConvert, Type resultType, Boolean recursion, PSObject originalValueToConvert, IFormatProvider formatProvider, TypeTable backupTable)
   at CallSite.Target(Closure, CallSite, Object)
   at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
   at System.Management.Automation.Interpreter.DynamicInstruction`2.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
    HResult        : -2146233087
CategoryInfo          : InvalidArgument: (:) [], RuntimeException
FullyQualifiedErrorId : InvalidCastConstructorException
InvocationInfo        : 
    ScriptLineNumber : 1
    OffsetInLine     : 1
    HistoryId        : -1
    Line             : [Statistics]$object
    PositionMessage  : At line:1 char:1
                       + [Statistics]$object
                       + ~~~~~~~~~~~~~~~~~~~
    CommandOrigin    : Internal
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1

Environment data

PS> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.3.7
PSEdition                      Core
GitCommitId                    7.3.7
OS                             Microsoft Windows 10.0.19044
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

Reactions are currently unavailable

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-BugIssue has been identified as a bug in the productIssue has been identified as a bug in the productResolution-No ActivityIssue has had no activity for 6 months or moreIssue has had no activity for 6 months or moreUp-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsWG-Enginecore PowerShell engine, interpreter, and runtimecore PowerShell engine, interpreter, and runtime

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.