Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ArgumentNullException when retrieving the default value of an Enum property defined with an integer #41498

Closed
icom85 opened this issue Aug 28, 2020 · 5 comments
Assignees
Milestone

Comments

@icom85
Copy link
Member

icom85 commented Aug 28, 2020

It is related to the DefaultValueAttribute, the Enums and the PropertyDescriptor class. The issue is in the static Enum.ToObject method. In order to observe the issue, it is necessary to use an integer as a default value of an enumeration. The code below easily reproduces it:

using System.ComponentModel;
using System.Windows.Forms;

namespace EnumDefaultValueIssue
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            var btn = new MyButton();
            var properties = TypeDescriptor.GetProperties(btn);
            var prop = properties["Dock"];

            // Crashes when calling ShouldSerializeValue on the PropertyDescriptor
            var shouldSerialize = prop.ShouldSerializeValue(btn);
        }
    }

    public class MyButton
    {
        //[DefaultValueAttribute(DockStyle.None)] //No crash
        [DefaultValueAttribute(0)] // Crash
        public DockStyle Dock { get;  set; }
    }
}

I`ve tested with the latest .NET 5.0 - 5.0.100-preview.8.20417.9, the issue is likely to be observed with other .NET Core runtimes. The same setup is working well in the full .NET framework.

Workaround:
Don't use the underlying integer type in the DefaultValueAttribute but rather the real enum member.

@Dotnet-GitSync-Bot
Copy link
Collaborator

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added the untriaged New issue has not been triaged by the area owner label Aug 28, 2020
@ghost
Copy link

ghost commented Aug 28, 2020

Tagging subscribers to this area: @safern
See info in area-owners.md if you want to be subscribed.

@joperezr
Copy link
Member

Moving to ComponentModel given the issue seems to be with the way that ReflectPropertyDescriptor is trying to get the default value of the object. This is not a regression, as I can at least repro with 3.1

@safern safern removed the untriaged New issue has not been triaged by the area owner label Aug 28, 2020
@safern safern added this to the 6.0.0 milestone Aug 28, 2020
@safern
Copy link
Member

safern commented Aug 28, 2020

Marking as 6.0.0 since it is not a regression. However I might be able to fix this while nullable annotating TypeConverters.

@safern safern self-assigned this Aug 28, 2020
@safern
Copy link
Member

safern commented Aug 10, 2021

This was fixed in: #55793

@safern safern closed this as completed Aug 10, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Sep 9, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants