Skip to content

Fix S1104 FP: Do not report in Unity serializable classes #7522

@antonioaversa

Description

@antonioaversa

Derived from this discuss post.

S1104 raises in all contexts, including in Unity scripts derived from MonoBehavior. The rule should not report in that context, however, due to how Unity works.
Using public properties would break core features of Unity such as YAML serialization and UI editing.

Unity requires using public fields, and using public properties would break core features of Unity such as serialization and UI editing.

Unity has specific rules for serialization, detailed here.

Serializers in Unity work directly on the fields of your C# classes rather than their properties, so there are rules that your fields must conform to be serialized.

Notice that there are ways of conforming to S1104 in Unity: as shown in the “Serialization of properties” section of the documentation, user-implemented properties with a backing field marked as SerializeField would be supported by the Unity Editor.

Regardless, those are corner cases that are rarely encountered in practice.

Metadata

Metadata

Labels

False PositiveRule IS triggered when it shouldn't be.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions