Serialize and deserialize exception in visual studio

Hi all,

i try to serialize and deserilaize object to bytearray and bytearray to object.
In grasshopper c# it works but not in visual studio.
I guess i have to mark the object class as seralizeable but i dont know how.

This method works in c# script in grasshopper.

  private byte[] ObjectToByteArray(object obj)
  {
    if (obj == null)
      return null;
    BinaryFormatter bf = new BinaryFormatter();
    MemoryStream ms = new MemoryStream();
    bf.Serialize(ms, obj);
    return ms.ToArray();
  }

But in VS i got this error
Capture

Does this error mean that every geometry class need to be marked as [SERIALIZABLE]?.