小编Goa*_*Goa的帖子

使用xml导出和导入自定义类型对象时出现问题

无法使用XML导出和导入自定义类型对象.对象更改类型并丢失方法.

脚本:

# 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
Run Code Online (Sandbox Code Playgroud)

我怎样才能$b.gettype() -eq $a.gettype()成真?

我想将对象导出到XML并重新导入它,而不会丢失其类型和方法.

powershell

4
推荐指数
1
解决办法
245
查看次数

标签 统计

powershell ×1