You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ArgumentNullException when retrieving the default value of an Enum property defined with an integer #41498
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:
usingSystem.ComponentModel;usingSystem.Windows.Forms;namespaceEnumDefaultValueIssue{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();varbtn=newMyButton();varproperties=TypeDescriptor.GetProperties(btn);varprop=properties["Dock"];// Crashes when calling ShouldSerializeValue on the PropertyDescriptorvarshouldSerialize=prop.ShouldSerializeValue(btn);}}publicclassMyButton{//[DefaultValueAttribute(DockStyle.None)] //No crash[DefaultValueAttribute(0)]// CrashpublicDockStyleDock{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.
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
Activity
Dotnet-GitSync-Bot commentedon Aug 28, 2020
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.
joperezr commentedon Aug 28, 2020
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 commentedon 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 commentedon Aug 10, 2021
This was fixed in: #55793