Trouble exporting and importing custom type objects using xml

Viewed 423

Trouble exporting and importing custom type objects using XML. The objects change type and lose methods.

Script:

# Powershell 5

$file = 'C:\Scripts\Storage\file.xml'

class test {
    [string] $name
    [string] getValue() {
        return 'value'
    }
}

$a = [test]::new()
$a.GetType()  # object type is "test"
$a |gm        # it has method "getValue" | Name       : getValue , 
MemberType : Method


$a | Export-Clixml $file
$b = Import-Clixml $file


$b.GetType()  # object type is "PSObject"
$b | gm       #  method "getValue" is no longer there

How do I get $b.gettype() -eq $a.gettype() to be true?

I want to export the object to XML and re-import it, without losing its type and methods.

1 Answers
Related