Get-Date는 DateTime 객체를 반환하는 것 같지만 BinarySerializer는 PSObject를 반환하는 것을 나타내는 이유가 무엇입니까?

StackOverflow https://stackoverflow.com//questions/9636698

문제

간단한 해시 테이블 :

$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()
.

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
.

이 메시지는 사라지고, 명시 적 캐스팅을 [DateTime]으로 사용하는 경우 다음과 같이

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

그래서 DateTime을 실제로 반환하지 않거나 여기에서 일하는 다른 PowerShell 이상한 것입니다.

도움이 되었습니까?

해결책

PowerShell의 모든 객체는 실제로 PSObject에서 대부분 투명하게 감겨졌습니다.PowerShell의 몇 가지 버그 이상이 있기 때문에 대부분 투명하게 말하기 때문에 래퍼를 다른 API로 누출하기 전에 래퍼를 제거 할 수 있습니다.이로 인해 모든 종류의 문제가 현재 보이는 것과 마찬가지입니다.Psobject 래퍼 용 connect.microsoft.com/powershell을 검색하십시오.나는 이것이 새로운 DLR 기반 엔진으로 v3에서 더 이상 문제가 아니라고 믿는다.

다른 팁

MSDN의 기본 :

psoobject 클래스 : Object 유형의 기본 오브젝트를 캡슐화하거나 PsSustOmObject를 입력하여 Windows PowerShell 환경 내의 오브젝트의 일관된보기를 허용합니다.

 ( 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
.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top