dotnet / winforms

Windows Forms is a .NET UI framework for building Windows desktop applications.
MIT License
4.29k stars 956 forks source link

Custom CodeDomSerializer causes casting exception during serialization in Designer #11430

Closed Jonasmarien closed 1 week ago

Jonasmarien commented 1 month ago

Environment

Version 17.9.7

.NET version

.NET 8

Did this work in a previous version of Visual Studio and/or previous .NET release?

No response

Issue description

When I create a custom CodeDomSerializer and specify it on a Component using the DesignerSerializerAttribute, a casting exception occurs whenever serialization is triggered in the Visual Studio Designer.

image

Project file

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net8.0-windows</TargetFramework>
    <Nullable>enable</Nullable>
    <UseWindowsForms>true</UseWindowsForms>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>

</Project>

Steps to reproduce

  1. Create a new WinForms project targeting .NET 8

  2. Add a WinForms UserControl image

  3. Create a new MyComponent class which inherits System.ComponentModel.Component

    internal class MyComponent : Component
    {
    }
  4. Create a new MySerializer class which inherits System.ComponentModel.Design.Serialization.CodeDomSerializer

    
    using System.ComponentModel.Design.Serialization;

public class MySerializer : CodeDomSerializer { }


5. Explicitly assign MySerializer to MyComponent using the `DesignerSerializerAttribute`

using System.ComponentModel; using System.ComponentModel.Design.Serialization;

[DesignerSerializer(typeof(MySerializer), typeof(CodeDomSerializer))] internal class MyComponent : Component { }


6.  Right-click MyControl and select 'View Designer'
![image](https://github.com/dotnet/winforms/assets/14920486/aea1c34b-5278-4e4b-a278-eb3c63bbec7f)

7. Open up Toolbox and drag MyComponent into the Designer view

**Note:** Here's a pre-made solution to start with. Simply start from step 6.
[https://github.com/Jonasmarien/winforms-codedomserializer-issue-reproduction](https://github.com/Jonasmarien/winforms-codedomserializer-issue-reproduction)

### Diagnostics

```text
[09:14:36.547921] fail: Request failures: Sessions/GetUpdatedRootComponentCodeDom.
                        Microsoft.DotNet.DesignTools.Client.DesignToolsServerException: Unable to cast object of type 'WInForms.CustomSerializer.Project.MySerializer' to type 'Microsoft.DotNet.DesignTools.Serialization.CodeDomSerializer'.

                        For information on how to troubleshoot the designer refer to the guide at https://aka.ms/winforms/designer/troubleshooting.
elachlan commented 1 month ago

@Olina-Zhang Can your team please test this?

Olina-Zhang commented 1 month ago

@Jonasmarien this issue can be resolved using Nuget: Microsoft.WinForms.Designer.SDK, and need to refactor the namespaces from System.ComponentModel.Design to Microsoft.DotNet.DesignTools.Designers, referring to blog: https://devblogs.microsoft.com/dotnet/custom-controls-for-winforms-out-of-process-designer/

Here is my updated app based on your steps: WinFormsApp22.zip

Video:

https://github.com/dotnet/winforms/assets/26474449/5e73df26-71fa-4ead-bbcc-7b3b4ae7fc2f

Jonasmarien commented 1 month ago

@Olina-Zhang Using the SDK does resolve the casting issue but causes another exception if I add the following to the serializer:

public class MySerializer : CodeDomSerializer
{
    public override object Serialize(IDesignerSerializationManager manager, object value)
    {
        SerializeToExpression(manager, value);
        return null;
    }
}

image

Same happens when I try to call the GetExpression method, even though both are defined in CodeDomSerializerBase.

Any idea why this exception is thrown?

Olina-Zhang commented 1 month ago

@Jonasmarien What about using component item in Winforms app directly? image

dotnet-policy-service[bot] commented 2 weeks ago

This submission has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 14 days.

It will be closed if no further activity occurs within 7 days of this comment.