如何使用powerhsell序列化对象? [英] How to use a powerhsell serialized object?

查看:40
本文介绍了如何使用powerhsell序列化对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有需要分析信息的机器.我使用 powershell 将信息收集到一个对象中,然后使用 Export-Clixml myObject.xml 将该对象写入文件.

I have machine that I need to analyze for information. I use powershell to gather the information into an object and then I write that object out to a file using Export-Clixml myObject.xml.

然后我尝试使用 $placeholder = Import-Clixml myObject.xml 测试导入这个对象,这很好用,因为我可以看到所有方法并访问方法中的数据.

I then try to test import this object using $placeholder = Import-Clixml myObject.xml and this works fine as I can see all the methods and access data in the methods.

但是,每当我尝试在需要此类对象的程序中使用此对象时,都会收到 Type Mismatch 错误.

However, whenever I try to use this object in my program where this type of object is required, I get a Type Mismatch error.

这对序列化对象来说是不可能的吗?

Is this not possible with serialized object?

推荐答案

如果您查看反序列化对象的完整类型名,您会发现它们以Deserialzed"开头.例如,如果我使用一堆 System.Diagnostics.Process 对象并使用 Export-Clixml 导出它们,当我使用 Import-Clixml 导入它们时,类型现在是 Deserialized.System.Diagnostics.Process 对象.PowerShell 正在做的是保存数据,然后让您再次访问它.您没有取回活动对象.通常唯一可用的方法是 ToString().这类似于通过远程处理序列化所发生的情况.如果您想要完全保真 .NET 类型序列化.我会使用 .NET 序列化程序之一,例如 BinarySerializer 或 XmlSerializer 或较新的 DataContractSerializer.

If you look at the full typename of the objects you deserialized you will see that they begin with "Deserialzed". For instance if I take a bunch of System.Diagnostics.Process objects and export them using Export-Clixml, when I use Import-Clixml to import them, the types are now Deserialized.System.Diagnostics.Process objects. What PowerShell is doing is saving the data out and then giving you access to that again. You're not getting back live objects. Typically the only method available is ToString(). This is similar to what happens with serialization over remoting. If you want full fidelity .NET type serialization. I would use one of the .NET serializers such as the BinarySerializer or the XmlSerializer or the newer DataContractSerializer.

这篇关于如何使用powerhsell序列化对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆