PSObject
을 일반적인 표현으로 변환하거나 PSObject.Properties
을 반복하고 리플렉션을 사용하여 POCO 필드를 채워야합니다.
Newtosoft.Json와이 간단한 직렬화 - 직렬화 코드는 첫 번째 방법을 구현하고 간단한 경우 잘 작동 할 수 있습니다
public class MyInfo
{
public string Name { get; set; }
public double Diff { get; set; }
public string File { get; set; }
}
static void Main(string[] args)
{
PSObject obj = PSObject.AsPSObject(new { Name = "David", Diff = 0.2, File = "output.txt" });
var serialized = JsonConvert.SerializeObject(obj.Properties.ToDictionary(k => k.Name, v => v.Value));
Console.WriteLine(serialized);
var deseialized = JsonConvert.DeserializeObject<MyInfo>(serialized);
Console.WriteLine($"Name: {deseialized.Name}");
Console.WriteLine($"Diff: {deseialized.Diff}");
Console.WriteLine($"File: {deseialized.File}");
}
출력 : NewtonSoft와
{"Name":"David","Diff":0.2,"File":"output.txt"}
Name: David
Diff: 0,2
File: output.txt
광고 원형 심판 .. 나는 것 또 한번 시도 해봐. –
일해 ... 좋았어! –