Home Dashboard Directory Help
Search

[Array] becomes [ArrayList] when imported via Import-CliXml by Ronald Rink


Status: 

Active


2
0
Sign in
to vote
Type: Bug
ID: 785187
Opened: 4/20/2013 5:43:09 AM
Access Restriction: Public
0
Workaround(s)
view
2
User(s) can reproduce this bug

Description

When using Export-CliXml/Import-CliXml to serialise/deserialise objects the object [Array] becomes an [ArrayList].
The problem with that is that the methods and members of both datatype do not behave exactly the same. For example, the "Length" property returns the number of entries (Count) on [Array] and the individual length of the the list items on [ArrayList]:

PS > $a = @("11", "22");
PS > $a.Length;
2

$al = New-Object System.Collections.ArrayList($null);
PS > $al.Add("11");
0
PS > $al.Add("22");
1
PS > $al.Length;
2
2

The following code demonstrates the error:

PS > $ht = @{};
PS > $ht.Array = @();
PS > $ht.Array.GetType();

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array

PS > $ht | Export-Clixml ht.xml;
PS > $ht2 = Import-Clixml ht.xml;
PS > $ht2.Array.GetType();

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     ArrayList                                System.Object

The via "Export-CliXml" generated XML file still contains the correct type information:
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
<Obj RefId="0">
<TN RefId="0">
<T>System.Collections.Hashtable</T>
<T>System.Object</T>
</TN>
<DCT>
<En>
<S N="Key">Array</S>
<Obj N="Value" RefId="1">
<TN RefId="1">
<T>System.Object[]</T>
<T>System.Array</T>
<T>System.Object</T>
</TN>
<LST />
</Obj>
</En>
</DCT>
</Obj>
</Objs>

Import-CliXml already breaks "ordered hashtables" as reported under "OrderedDictionary becomes Hashtable when exported to and imported from clixml" [https://connect.microsoft.com/PowerShell/feedback/details/771721/ordereddictionary-becomes-hashtable-when-exported-to-and-imported-from-clixml]

It does not matter if you are using "Set-StrictMode" with any PowerShell version number. See also "[Array] becomes [ArrayList] when imported via Import-CliXml" [http://d-fens.ch/2013/04/20/array-becomes-arraylist/trackback/].
Details
Sign in to post a comment.
Posted by SSG31415926 on 5/21/2014 at 6:24 AM
There are other inconsistencies. For example

Import-CliXml -Path $thisFilePath | measure

returns

Count    : 1
:

whereas:

(Import-CliXml -Path $thisFilePath) | measure

returns a count that depends on the number of objects in the file.

Compare that with Import-CSV, both

Import-Csv -Path $testCSVPath | measure

and

(Import-Csv -Path $testCSVPath) | measure

return a count based on the number of rows in the CSV.
Sign in to post a workaround.