Skip to content

Commit

Permalink
Fix case of DefaultValue using enum backing type. (#55793)
Browse files Browse the repository at this point in the history
  • Loading branch information
ericstj authored Jul 16, 2021
1 parent 1d065b6 commit b8c799d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@

namespace System.ComponentModel.Tests
{
internal enum DescriptorTestEnum
{
Value0 = 0,
Value1 = 1
}

internal class DescriptorTestComponent : IComponent, ISite
{
private Dictionary<Type, object> _services;
Expand All @@ -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; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down

0 comments on commit b8c799d

Please sign in to comment.