Error ‘Code generation for property failed. Error was Type in assembly is not marked as serializable’ when trying to add/change anything on WinForm


The full error message is as follow:

Code generation for property ‘Structures’ failed. Error was Type ‘Placeables.Placeable’ in assembly ‘SPAA, Version=, Culture=neutral, PublicKeyToken=null’ is not marked as serializable.

I’m using Visual Studio 2019 and this happens every time I try to add or change something on the MainForm by dragging and dropping from the toolbox into the design, e.g. adding a new background worker or changing the name of a existing timer in it’s proprieties. As you can see it’s nothing directly related to the ‘Placeable’ type shown in the error because I’m not referencing it on the new component, it is empty. I can’t even save the project after adding any changes without facing this error.

If I try to create the same empty background worker in the designer file and reference the handler by hand the project saves, builds and runs normally.

The Placeable type is not Serializable, the only thing different on it is that it implements an IComparable (public abstract class Placeable : IComparable) and I could only find the Structure attribute on the namespace where the Structures are being implemented (namespace SPAA.Placeables.Structures).

I’ve also seen in threads like this the use of a NonSerializedAttribute() tag on a EventHandler, but the only event handler on this class is a MouseOver (public event EventHandler MouseOver;) which I’ve tried adding the tag (and making the class Serializable at the same time) with no success.

I also tried restarting the IDE and reopening the solution.

I also tried opening with another IDE, I used Rider but the design of the MainForm does not load on Rider, I can edit the code but the visual window with the drag and drop toolbox doesn’t load but that is a problem for another time.

I truly don’t know where the problem lies, if is in the Visual Studio Settings, the framework file maybe (?), if it really is in the huge Placeables/MainForm classes, and so on.

I appreciate any kind of help on this one guys, thanks.

Source: Visual Studio Questions