How to use a powerhsell serialized object?

↘锁芯ラ 提交于 2019-12-08 07:41:00

问题


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.

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.

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?


回答1:


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.



来源:https://stackoverflow.com/questions/18747942/how-to-use-a-powerhsell-serialized-object

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