Serializing non-Rhino classes to a Rhino File

You don’t want to use a FileStream because you want to serialize to the 3DM file. Use a MemoryStream. Then, you can archive the bytes using RhinoCommon’s BinaryArchiveWriter and BinaryArchiveReader objects.

I haven’t tested this, but it might work:

[Serializable]
class StuartData
{
  /// <summary>
  /// Members
  /// </summary>
  public double X { get; set; }
  public double Y { get; set; }
  public double Z { get; set; }

  /// <summary>
  /// Constructor
  /// </summary>
  public StuartData()
  {
  }

  /// <summary>
  /// Constructor
  /// </summary>
  public StuartData(StuartData src)
  {
    this.X = src.X;
    this.Y = src.Y;
    this.Z = src.Z;
  }

  /// <summary>
  /// Create
  /// </summary>
  public void Create(StuartData src)
  {
    this.X = src.X;
    this.Y = src.Y;
    this.Z = src.Z;
  }

  /// <summary>
  /// Write to binary archive
  /// </summary>
  public bool Write(BinaryArchiveWriter archive)
  {
    bool rc = false;
    if (null != archive)
    {
      try
      {
        // Write chunk version
        archive.Write3dmChunkVersion(1, 0);

        // Write 'this' object
        IFormatter formatter = new BinaryFormatter();
        MemoryStream stream = new MemoryStream();
        formatter.Serialize(stream, this);
        stream.Seek(0, 0);
        byte[] bytes = stream.ToArray();
        archive.WriteByteArray(bytes);
        stream.Close();

        // Verify writing
        rc = archive.WriteErrorOccured;
      }
      catch
      {
        // TODO
      }
    }
    return rc;
  }

  /// <summary>
  /// Read from binary archive
  /// </summary>
  public bool Read(BinaryArchiveReader archive)
  {
    bool rc = false;
    if (null != archive)
    {
      // Read and verify chunk version
      int major, minor;
      archive.Read3dmChunkVersion(out major, out minor);
      if (1 == major && 0 == minor)
      {
        try
        {
          // Read this object
          byte[] bytes = archive.ReadByteArray();
          MemoryStream stream = new MemoryStream(bytes);
          IFormatter formatter = new BinaryFormatter();
          StuartData data = formatter.Deserialize(stream) as StuartData;
          this.Create(data);
            
          // Verify reading
          rc = archive.ReadErrorOccured;
        }
        catch
        {
          // TODO
        }
      }
    }
    return rc;
  }
}