4
使用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
我怎麼$b.gettype() -eq $a.gettype()
是真的嗎?
我想將對象導出到XML並重新導入它,而不會丟失它的類型和方法。
謝謝:) 對於這樣一個明確的答案:) 解決 – Goa