I'm trying to use PowerShell's classes that is very handy way of grouping related data together and facing quite tedious to deal with behavior. The simplified scenario: one PS script that defines class and another script that uses that class.
Common.ps1
class X
{
[string] $A
}
Script1.ps1
. $PSScriptRootCommon.ps1
[X] $v = New-Object X
Everything is good - you can run Script1.ps1
arbitrary amount of times with no issues - until you make any change in Common.ps1
. You will face the following error.
Cannot convert the "X" value of type "X" to type "X".
At D:empPSIssueScript1.ps1:3 char:1
+ [X] $v = New-Object X
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : MetadataError: (:) [], ArgumentTransformationMetadataException
+ FullyQualifiedErrorId : RuntimeException
Conceivably any change (even if you just added whitespace) in PS file forces its recompilation, so that type X
becomes different than X
it used to be - temporary container assembly has been changed (the very same issue easily reproducible in .NET - types are identical as long as "Fully Qualified Assembly Names" are the same). Change in Script1.ps1
makes things function normally again.
Is there any way to overcome such kind of issues?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…