NotSupportedException when deserializing DBNull from RESX
See original GitHub issueDescription
Hello,
My name is Norton and I’m writing on behalf of the WinForms team at DevExpress.
We came across the following issue:
If DBNull.Value
is assigned to a property of a component in the WinForms designer, it is stored in the RESX file associated with the form. If the same form is opened afterward, a SerializationException
is thrown on an attempt to call the ResourceManager.GetObject
method in the designer-generated code.
Reproduction Steps
- Install Visual Studio 2022 with the “.NET desktop development” workload.
- Open Visual Studio 2022 and create a new project using the “Windows Forms App [C#]” template (further template configuration doesn’t matter).
- Once the project is created, close the designer tab without waiting for it to load.
- Open Form1.Designer.cs and add
this.Tag = DBNull.Value;
at the end of theInitializeComponent
method. - Open the form designer by double-clicking Form1.cs in the Solution Explorer.
- Resize the form, save the file and close the tab.
This forces the form designer to regenerate Form1.Designer.cs, store
DBValue.Null
inForm1.resx
and generate a call to theResourceManager.GetObject
method for that value. - Run the project.
I attached a project created using these steps - it is sufficient to simply run it. T1143850c.zip
These steps do not represent a realistic use case where this issue occurs, but the fastest way to reproduce it. A realistic use case would involve creating a custom control with a custom property initialized with DBNull.Value
. If necessary, I can add steps and a project for this use case.
Expected behavior
The application launches without any exceptions and an empty form is displayed.
Actual behavior
An exception is thrown.
Exception thrown: 'System.NotSupportedException' in System.Private.CoreLib.dll
Only one DBNull instance may exist, and calls to DBNull deserialization methods are not allowed.
Regression?
This issue does not occur in .NET Framework.
Known Workarounds
We are not aware of workarounds that do not sacrifice usability (such as disabling serialization of the property completely with DesignerSerializationVisibilityAttribute
).
Configuration
No response
Other information
We are not certain whether the issue lies in the way the WinForms designer or .NET SDK serializes the value, or the way the standard .NET serialization API serializes/deserializes it, but we consider the latter more likely, based on our findings. If this happens to be an issue with a different product, please let me know.
It’s also possible that this use case is not supported in general, although we have several reasons to believe why this is not the case:
- The same use case works as expected in .NET Framework applications (i.e. this behavior is a regression).
- With this limitation, it isn’t possible to define a serializable component property whose default value is
DBNull.Value
, which we consider a valid use case. - The
DBNull
type is still marked withSerializableAttribute
, and it’s stated in https://github.com/dotnet/runtime/issues/23253 that support for its serialization was explicitly added.
Issue Analytics
- State:
- Created 8 months ago
- Comments:10 (7 by maintainers)
Top GitHub Comments
@ericstj I do, so that shouldn’t be a problem (aside from other considerations).
Thanks everyone. So, from what I understand, it is related to the WinForms designer after all.
Should I re-open this issue in the designer repo? Or, better yet, is it possible to move the issue there?
P.S. Case in point, the issue can also be reproduced with the .NET Framework out-of-process designer.