Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fix case of DefaultValue using enum backing type.
  • Loading branch information
ericstj committed Jul 16, 2021
commit 5d22c664ee9b8cf53280027ed2db7c256a1b68c5
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