Prerequisites
Steps to reproduce
$code = '
class Example : System.IConvertible
{
[string] ToString () { Return "object.ToString" }
[string] ToString ([System.IFormatProvider]$fmt) { Return "IConvertible.ToString" }
[TypeCode] GetTypeCode() { Return [TypeCode]::String }
[object] ToType ([type]$type, [System.IFormatProvider]$fmt) { Throw "IConvertible.ToType" }
';
$code += (@('Boolean', 'Char', 'SByte', 'Byte',
'Int16', 'UInt16', 'Int32', 'UInt32', 'Int64', 'UInt64',
'Single', 'Double', 'Decimal', 'DateTime' # , 'String'
) | ForEach-Object {
"[$_] To$_ ([System.IFormatProvider]`$fmt) { Throw 'IConvertible.To$_' }"
}) -join "`n";
$code += '
}
[Example]::new().ToString(); # object.ToString
"" + [Example]::new(); # PSObject.ToString -> InvalidCastException
';
Write-Verbose $code;
Invoke-Expression $code;
Expected behavior
Outputs object.ToString twice.
Actual behavior
object.ToString
OperationStopped:
Line |
25 | "" + [Example]::new(); # PSObject.ToString -> InvalidCastExcept …
| ~~~~~~~~~~~~~~~~~~~~~
| Unable to cast object of type 'Example' to type 'System.String'.
Error details
The technical analysis is in the first comment by me, rendering the error details irrelevant.
Environment data
Name Value
---- -----
PSVersion 7.6.6
PSEdition Core
GitCommitId 7.6.6
OS Microsoft Windows 10.0.26200
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.4
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Visuals
No response
Prerequisites
Steps to reproduce
Expected behavior
Outputs
object.ToStringtwice.Actual behavior
Error details
The technical analysis is in the first comment by me, rendering the error details irrelevant.
Environment data
Visuals
No response