以简单的HashTable为例:
$data = @{
First = 'Justin';
Last = 'Dearing';
StartDate = Get-Date '2002-03-23';
}
关键 StartDate 似乎包含一个DateTime。
C:\Users\zippy\Documents> $data.StartDate.GetType().FullName
System.DateTime
但是,如果我尝试对其执行二进制序列化,则会收到一个异常,抱怨PSObject不可序列化。
$ms = New-Object System.IO.MemoryStream
$bf = New-Object System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
$bf.Serialize($ms, $data)
$ms.Close()
抛出:
DocumentsException calling "Serialize" with "2" argument(s): "Type 'System.Management.Automation.PSObject' in Assembly 'System.Management.Automation, Versio
n=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' is not marked as serializable."
At C:\Users\jdearing\AppData\Local\Temp\b8967f99-0a24-41f7-9c97-dad2bc288bd9.ps1:12 char:14
+ $bf.Serialize <<<< ($ms, $data)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
如果我像这样对 [DateTime] 使用显式强制转换,这条消息就会消失,一切正常:
$data = @{
First = 'Justin';
Last = 'Dearing';
StartDate = [DateTime] (Get-Date '2002-03-23');
}
Get-Date 是不是真的返回了 DateTime,或者是其他一些 powershell 奇怪的东西在这里工作。