.netwinformsserialization

Configure appropriate serialization for Windows forms


After upgrading my win-forms-project to .NET 9 I now get the following error on the properties of my user-controls:

WFO1000: Property 'property' does not configure the code serialization for its property content.

Then I read the explanation from microsoft to that issue: https://learn.microsoft.com/en-us/dotnet/core/compatibility/windows-forms/9.0/security-analyzers

Now I understand the security-issues but I still do not know how to fix that. I do not want to supress this message, I want to handle this serialization-issue properly.

In that article Microsoft suggests:

Review the properties flagged by the analyzer and configure appropriate serialization settings as needed.

Now I wonder: How exactly do I do that?


Solution

  • This happens, e.g., when you create your own WinForms control. Declare an appropriate attribute on the affected properties. E.g.:

    If you don't want to save (i.e., serialize) this property:

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public Color FocusColor { get; set; } = Color.Yellow;
    

    In this case it is probably also a good idea to set [Browsable(false)], which hides this property from the property editor, because edits in the property editor will not be persisted.

    or

    If you want to save (i.e., serialize) this property:

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public Color FocusColor { get; set; } = Color.Yellow;
    

    For an object whose properties must be serialized you can use the value DesignerSerializationVisibility.Content.