Why does Get-Date seem to return DateTime objects, but the BinarySerializer indicate it returns a PSObject?

倾然丶 夕夏残阳落幕 提交于 2019-12-04 03:27:06

问题


Take the simple HashTable:

$data = @{
    First = 'Justin';
    Last = 'Dearing';
    StartDate = Get-Date '2002-03-23';
}

The key StartDate seems to contain a DateTime.

C:\Users\zippy\Documents> $data.StartDate.GetType().FullName
System.DateTime

However, if I attempt to perform binary serialization on it, I get an exception complaining that PSObject is not serializable.

$ms = New-Object System.IO.MemoryStream
$bf = New-Object System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
$bf.Serialize($ms, $data)
$ms.Close()

Throws:

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

This message goes away and everything works if I use an explicit cast to [DateTime] like so:

$data = @{
    First = 'Justin';
    Last = 'Dearing';
    StartDate = [DateTime] (Get-Date '2002-03-23');
}

So is Get-Date not really returning a DateTime, or is some other powershell oddity at work here.


回答1:


Every object in powershell is actually wrapped mostly transparently in a psobject. I say mostly transparently because there are more than a few bugs in powershell that omit to remove the wrapper before leaking the object to another API. This causes all sorts of issues, much like the one you see now. Search connect.microsoft.com/powershell for psobject wrapper. I believe this is no longer an issue in v3 with the new DLR based engine.




回答2:


Base on the msdn:

PSOobject Class : Encapsulates a base object of type Object or type PSCustomObject to allow for a consistent view of any object within the Windows PowerShell environment.

 ( get-Date '2002-03-23' ) -IS [psobject]
True

( get-Date '2002-03-23' ) -IS [datetime]
True

[datetime]( get-Date '2002-03-23' ) -IS [datetime]
True

[datetime]( get-Date '2002-03-23' ) -IS [psobject]
False


来源:https://stackoverflow.com/questions/9636698/why-does-get-date-seem-to-return-datetime-objects-but-the-binaryserializer-indi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!