gpt4 book ai didi

c# - Protobuf-net 中没有为类型 : System. Management.Automation.PSObject 定义序列化器

转载 作者:太空狗 更新时间:2023-10-30 01:05:13 25 4
gpt4 key购买 nike

这是我的类(class)

  [ProtoContract]
internal class Powershellresults
{
internal Powershellresults()
{
}
[ProtoMember(1)]
public Collection<PSObject> PsObjects { get; set; }
[ProtoMember(2)]
public string Script { get; set; }
[ProtoMember(3)]
public string Viewname { get; set; }
}

但我没有为类型定义序列化程序:System.Management.Automation.PSObject当我尝试序列化它时

private byte[] SerializeResults(Powershellresults obj)
{
byte[] data;
using (var ms = new MemoryStream())
{
ProtoBuf.Serializer.Serialize(ms, obj);
data = ms.ToArray(); ;
}
return data;
}

PsObject 是 System.Management.Automation 的一部分

我可以解决这个问题并实际序列化这个集合吗?

更新:通过添加到我的序列化方法

RuntimeTypeModel.Default.Add(typeof(PSObject), true);

我可以让它序列化——但是一旦序列化它就会丢失大部分字段我如何让它“克隆”原始对象以保留其所有字段和属性?

编辑

我想通了!我心想,Powershell 必须能够序列化它的对象,因为您可以轮询远程机器——否则它还能如何在机器之间发送 PSObject——它做到了! (呜呜!!!)

嗯,我找到了一篇描述这个的文章,并找到了我的关键词System.Management.Automation.PSSerializer

Collection<PSObject> PSCol = Powershell.Invoke(); 
string SerializedCollection = PSSerializer.Serialize(PSCol);

//XML序列化完成!

然后使用 Protobuf-net 或其他方式序列化字符串...

反序列化有点笨拙但非常简单反序列化使用:使用 Protobuf 反序列化字符串,然后...

PSObject obj = PSSerializer.Deserialize(DeserializedString) as PSObject;

Collection<PSObject> DeserializedCollection = new Collection<PSObject>(((ArrayList)obj.ImmediateBaseObject).Cast<PSObject>().ToArray());

就是这样..

最佳答案

我想通了!我心想,Powershell 必须能够序列化它的对象,因为您可以轮询远程机器——否则它还能如何在机器之间发送 PSObject——它做到了! (呜呜!!!)

嗯,我找到了一篇描述这个的文章,并找到了我的关键词System.Management.Automation.PSSerializer

Collection<PSObject> PSCol = Powershell.Invoke(); 
string SerializedCollection = PSSerializer.Serialize(PSCol);

//XML序列化完成!

然后使用 Protobuf-net 或其他方式序列化字符串...

反序列化有点笨拙但非常简单反序列化使用:使用 Protobuf 反序列化字符串,然后...

PSObject obj = PSSerializer.Deserialize(DeserializedString) as PSObject;

Collection<PSObject> DeserializedCollection = new Collection<PSObject>(((ArrayList)obj.ImmediateBaseObject).Cast<PSObject>().ToArray());

就是这样..

关于c# - Protobuf-net 中没有为类型 : System. Management.Automation.PSObject 定义序列化器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19811921/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com