-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Closed
Labels
Needs-TriageThe issue is new and needs to be triaged by a work group.The issue is new and needs to be triaged by a work group.Resolution-No ActivityIssue has had no activity for 6 months or moreIssue has had no activity for 6 months or more
Description
Prerequisites
- Write a descriptive title.
- Make sure you are able to repro it on the latest released version
- Search the existing issues.
- Refer to the FAQ.
- Refer to Differences between Windows PowerShell 5.1 and PowerShell.
Steps to reproduce
Casting from PSCustomObject to Object works but doing the same thing in a Class fails
Shouldn't it basically work and do no conversion as PSCustomObject BaseType is Object?
$PsO = [PSCustomObject]@{
Name = 'Value'
}
# PSCustomObject to Object works:
[Object][PSCustomObject]$PsO
class TestClass {
$PsO
[string]$Ok
}
class TestClassP {
[PSCustomObject]$PsO
[string]$Ok
}
$Class = [TestClass]@{
PsO = $PsO
Ok = 'Yes'
}
$Generic = $Class | ConvertTo-Json -Depth 10 | ConvertFrom-Json # Convert to PSCustomObject
# Cannot convert the "@{Name=Value}" value of type "System.Management.Automation.PSCustomObject" to type "System.Object":
[TestClass]$Generic
# Works (stay as PSCustomObject)
[TestClassP]$Generic
$Generic = $Class | ConvertTo-Json -Depth 10 | ConvertFrom-Json -AsHashtable # Convert to OrderedHashtable
# Works (stay as OrderedHashtable)
[TestClass]$GenericExpected behavior
Class cast successfully, "PsO" doesn't change.Actual behavior
ErrorError details
Exception :
Type : System.Management.Automation.RuntimeException
ErrorRecord :
Exception :
Type : System.Management.Automation.ParentContainsErrorRecordException
Message : Cannot convert value "@{PsO=; Ok=Yes}" to type "TestClass". Error: "Cannot convert value "@{Name=Value}" to type "System.Object". Error: "Cannot convert the
"@{Name=Value}" value of type "System.Management.Automation.PSCustomObject" to type "System.Object".""
HResult : -2146233087
CategoryInfo : InvalidArgument: (:) [], ParentContainsErrorRecordException
FullyQualifiedErrorId : InvalidCastConstructorException
InvocationInfo :
ScriptLineNumber : 1
OffsetInLine : 1
HistoryId : -1
Line : [TestClass]$Generic
PositionMessage : At line:1 char:1
+ [TestClass]$Generic
+ ~~~~~~~~~~~~~~~~~~~
CommandOrigin : Internal
ScriptStackTrace : at <ScriptBlock>, <No file>: line 1
Message : Cannot convert value "@{PsO=; Ok=Yes}" to type "TestClass". Error: "Cannot convert value "@{Name=Value}" to type "System.Object". Error: "Cannot convert the "@{Name=Value}"
value of type "System.Management.Automation.PSCustomObject" to type "System.Object".""
InnerException :
Type : System.Management.Automation.PSInvalidCastException
ErrorRecord :
Exception :
Type : System.Management.Automation.ParentContainsErrorRecordException
Message : Cannot convert value "@{PsO=; Ok=Yes}" to type "TestClass". Error: "Cannot convert value "@{Name=Value}" to type "System.Object". Error: "Cannot convert the
"@{Name=Value}" value of type "System.Management.Automation.PSCustomObject" to type "System.Object".""
HResult : -2146233087
CategoryInfo : InvalidArgument: (:) [], ParentContainsErrorRecordException
FullyQualifiedErrorId : InvalidCastConstructorException
InvocationInfo :
ScriptLineNumber : 1
OffsetInLine : 1
HistoryId : -1
Line : [TestClass]$Generic
PositionMessage : At line:1 char:1
+ [TestClass]$Generic
+ ~~~~~~~~~~~~~~~~~~~
CommandOrigin : Internal
ScriptStackTrace : at <ScriptBlock>, <No file>: line 1
TargetSite :
Name : Convert
DeclaringType : System.Management.Automation.LanguagePrimitives+ConvertViaNoArgumentConstructor, System.Management.Automation, Version=7.3.3.500, Culture=neutral,
PublicKeyToken=31bf3856ad364e35
MemberType : Method
Module : System.Management.Automation.dll
Message : Cannot convert value "@{PsO=; Ok=Yes}" to type "TestClass". Error: "Cannot convert value "@{Name=Value}" to type "System.Object". Error: "Cannot convert the
"@{Name=Value}" value of type "System.Management.Automation.PSCustomObject" to type "System.Object".""
Data : System.Collections.ListDictionaryInternal
InnerException :
Type : System.Management.Automation.PSInvalidCastException
ErrorRecord :
Exception :
Type : System.Management.Automation.ParentContainsErrorRecordException
Message : Cannot convert value "@{Name=Value}" to type "System.Object". Error: "Cannot convert the "@{Name=Value}" value of type
"System.Management.Automation.PSCustomObject" to type "System.Object"."
HResult : -2146233087
CategoryInfo : InvalidArgument: (:) [], ParentContainsErrorRecordException
FullyQualifiedErrorId : InvalidCastConstructorException
TargetSite :
Name : Convert
DeclaringType : System.Management.Automation.LanguagePrimitives+ConvertViaNoArgumentConstructor, System.Management.Automation, Version=7.3.3.500, Culture=neutral,
PublicKeyToken=31bf3856ad364e35
MemberType : Method
Module : System.Management.Automation.dll
Message : Cannot convert value "@{Name=Value}" to type "System.Object". Error: "Cannot convert the "@{Name=Value}" value of type "System.Management.Automation.PSCustomObject"
to type "System.Object"."
InnerException :
Type : System.Management.Automation.PSInvalidCastException
ErrorRecord :
Exception :
Type : System.Management.Automation.ParentContainsErrorRecordException
Message : Cannot convert the "@{Name=Value}" value of type "System.Management.Automation.PSCustomObject" to type "System.Object".
HResult : -2146233087
CategoryInfo : InvalidArgument: (:) [], ParentContainsErrorRecordException
FullyQualifiedErrorId : ConvertToFinalInvalidCastException
TargetSite :
Name : SetObjectProperties
DeclaringType : System.Management.Automation.LanguagePrimitives
MemberType : Method
Module : System.Management.Automation.dll
Message : Cannot convert the "@{Name=Value}" value of type "System.Management.Automation.PSCustomObject" to type "System.Object".
InnerException :
Type : System.InvalidOperationException
TargetSite :
Name : CreateMemberNotFoundError
DeclaringType : System.Management.Automation.LanguagePrimitives
MemberType : Method
Module : System.Management.Automation.dll
Message : The property 'Name' was not found for the 'System.Object' object. There is no settable property available.
Source : System.Management.Automation
HResult : -2146233079
StackTrace :
at System.Management.Automation.LanguagePrimitives.CreateMemberNotFoundError(PSObject pso, DictionaryEntry property, Type resultType)
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)
Source : System.Management.Automation
HResult : -2147467262
StackTrace :
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.ConvertPSObjectToType(PSObject valueToConvert, Type resultType, Boolean recursion, IFormatProvider formatProvider, 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 : [TestClass]$Generic
PositionMessage : At line:1 char:1
+ [TestClass]$Generic
+ ~~~~~~~~~~~~~~~~~~~
CommandOrigin : Internal
ScriptStackTrace : at <ScriptBlock>, <No file>: line 1Environment data
Name Value
---- -----
PSVersion 7.3.3
PSEdition Core
GitCommitId 7.3.3
OS Linux 5.15.0-67-generic #74-Ubuntu SMP Wed Feb 22 14:14:39 UTC 2023
Platform Unix
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0Visuals
No response
mklement0
Metadata
Metadata
Assignees
Labels
Needs-TriageThe issue is new and needs to be triaged by a work group.The issue is new and needs to be triaged by a work group.Resolution-No ActivityIssue has had no activity for 6 months or moreIssue has had no activity for 6 months or more