title: No serializer defined for type: System.Management.Automation.PSObject in Protobuf-net
body: <p>this is my class</p>
  
  <pre><code>  [ProtoContract]
              internal class Powershellresults
              {
                  internal Powershellresults()
                  {
                  }
                  [ProtoMember(1)]
                  public Collection&lt;PSObject&gt; PsObjects { get; set; }
                  [ProtoMember(2)]
                  public string Script { get; set; }
                  [ProtoMember(3)]
                  public string Viewname { get; set; }
              }
  </code></pre>
  
  <p>but i get No serializer defined for type: System.Management.Automation.PSObject 
  when i try to serialize it</p>
  
  <pre><code>private byte[] SerializeResults(Powershellresults obj)
          {
              byte[] data;
              using (var ms = new MemoryStream())
              {
                  ProtoBuf.Serializer.Serialize(ms, obj);
                  data = ms.ToArray(); ;
              }
              return data;
          }
  </code></pre>
  
  <p>PsObject is part of System.Management.Automation</p>
  
  <p>Can i get around this and actually serialize this collection?</p>
  
  <p>UPDATE:
  by adding to my serialization method</p>
  
  <pre><code>RuntimeTypeModel.Default.Add(typeof(PSObject), true);
  </code></pre>
  
  <p>I can get it to serialize- but it loses most of it's fields once serialized
  how do i get it to "clone" the original object so it keeps all of its fields and properties?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I figured it out!
  I thought to myself that Powershell had to be able to serialize it's objects, because you can poll remote machines - how else would it be able to send the PSObject between machines - which it does! (woohoo!!!!)</p>
  
  <p>Well, i found an article that described this, and found my keyword <a href="http://msdn.microsoft.com/en-us/library/system.management.automation.psserializer_members%28v=vs.85%29.ASPX" rel="nofollow">System.Management.Automation.PSSerializer</a></p>
  
  <pre><code>Collection&lt;PSObject&gt; PSCol = Powershell.Invoke(); 
  string SerializedCollection = PSSerializer.Serialize(PSCol);
  </code></pre>
  
  <p>//serialization to XML done!</p>
  
  <p>then Serialize the string using Protobuf-net or what ever...</p>
  
  <p>Deserialization is a bit awkward but pretty straight forward
  for deserialization use:
  Use Protobuf to deserialize the string and then...</p>
  
  <pre><code>PSObject obj = PSSerializer.Deserialize(DeserializedString) as PSObject;
  
  Collection&lt;PSObject&gt; DeserializedCollection = new Collection&lt;PSObject&gt;(((ArrayList)obj.ImmediateBaseObject).Cast&lt;PSObject&gt;().ToArray());
  </code></pre>
  
  <p>and thats it.. </p>