Skip to content

Possible mis application of FormerlySerializedAs attribute to multiple fields

Matt Ellis edited this page Feb 19, 2019 · 2 revisions

This inspection will warn if the [FormerlySerializedAs] attribute is applied to multiple fields, in the following syntax:

[FormerlySerializedAs("value")] public int value1, value2, value3;

When an attribute is applied to a field declaration that is declaring multiple fields, the attribute is applied to each field, and is equivalent to:

[FormerlySerializedAs("value")] public int value1;
[FormerlySerializedAs("value")] public int value2;
[FormerlySerializedAs("value")] public int value3;

The [FormerlySerializedAs] attribute is not intended to be used in this manner, and should only be applied to a single field declaration.

The inspection provides a Alt+Enter quick fix to split the declarations, so that the [FormerlySerializedAs] attribute can be explicitly set on the required fields.

See the documentation for more details on the intended usage of the [FormerlySerializedAs] attribute.

This inspection was first added in Rider/ReSharper 2018.2.

Clone this wiki locally