diff --git a/src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/ReflectPropertyDescriptor.cs b/src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/ReflectPropertyDescriptor.cs index 873ad225e3e36..096fef344ad07 100644 --- a/src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/ReflectPropertyDescriptor.cs +++ b/src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/ReflectPropertyDescriptor.cs @@ -300,7 +300,7 @@ private object DefaultValue object? defaultValue = ((DefaultValueAttribute)a).Value; bool storedAsUnderlyingType = defaultValue != null && PropertyType.IsEnum && PropertyType.GetEnumUnderlyingType() == defaultValue.GetType(); _defaultValue = storedAsUnderlyingType ? - Enum.ToObject(PropertyType, _defaultValue!) : + Enum.ToObject(PropertyType, defaultValue!) : defaultValue; } else diff --git a/src/libraries/System.ComponentModel.TypeConverter/tests/DescriptorTestComponent.cs b/src/libraries/System.ComponentModel.TypeConverter/tests/DescriptorTestComponent.cs index e8f2f590c5c09..2602b560a4d6b 100644 --- a/src/libraries/System.ComponentModel.TypeConverter/tests/DescriptorTestComponent.cs +++ b/src/libraries/System.ComponentModel.TypeConverter/tests/DescriptorTestComponent.cs @@ -5,6 +5,12 @@ namespace System.ComponentModel.Tests { + internal enum DescriptorTestEnum + { + Value0 = 0, + Value1 = 1 + } + internal class DescriptorTestComponent : IComponent, ISite { private Dictionary _services; @@ -17,6 +23,9 @@ internal class DescriptorTestComponent : IComponent, ISite [DefaultValue(DefaultPropertyValue)] public int Property { get; set; } + [DefaultValue(0)] + public DescriptorTestEnum EnumProperty { get; set; } + public object PropertyWhichThrows { get { throw new NotImplementedException(); } } public string StringProperty { get; private set; } diff --git a/src/libraries/System.ComponentModel.TypeConverter/tests/PropertyDescriptorTests.cs b/src/libraries/System.ComponentModel.TypeConverter/tests/PropertyDescriptorTests.cs index 69f4a86bbf629..f791d46824f82 100644 --- a/src/libraries/System.ComponentModel.TypeConverter/tests/PropertyDescriptorTests.cs +++ b/src/libraries/System.ComponentModel.TypeConverter/tests/PropertyDescriptorTests.cs @@ -144,6 +144,17 @@ public void ShouldSerializeValueReturnsTrueWhenValueIsNotDefault() Assert.True(propertyDescriptor.ShouldSerializeValue(component)); } + [Fact] + public void ShouldSerializeValueHandlesEnumWithBackingTypeDefaultValue() + { + var component = new DescriptorTestComponent(); + component.EnumProperty = DescriptorTestEnum.Value1; + var properties = TypeDescriptor.GetProperties(component.GetType()); + PropertyDescriptor propertyDescriptor = properties.Find(nameof(component.EnumProperty), false); + + Assert.True(propertyDescriptor.ShouldSerializeValue(component)); + } + [Fact] public static void ReadOnlyPropertyReturnsTrue() {