Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit adf6473

Browse files
stephentoubdanmoseley
authored andcommitted
Fix EnumConverter porting bug around ArrayList (#19475)
EnumConverter.GetStandardValues switched from using `ArrayList` to `List<object>` when it was ported to .NET Core. But it performs sorting using the non-generic IComparer, which `List<object>` doesn't have APIs for. Rather than trying to work around the difference, it makes sense to just revert to using ArrayList.
1 parent ac3a4cb commit adf6473

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/System.ComponentModel.TypeConverter/src/System/ComponentModel/EnumConverter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,11 @@ public override StandardValuesCollection GetStandardValues(ITypeDescriptorContex
228228
Type reflectType = TypeDescriptor.GetReflectionType(EnumType) ?? EnumType;
229229

230230
FieldInfo[] fields = reflectType.GetFields(BindingFlags.Public | BindingFlags.Static);
231-
List<object> objValues = null;
231+
ArrayList objValues = null;
232232

233233
if (fields != null && fields.Length > 0)
234234
{
235-
objValues = new List<object>(fields.Length);
235+
objValues = new ArrayList(fields.Length);
236236
}
237237

238238
if (objValues != null)
@@ -271,7 +271,7 @@ public override StandardValuesCollection GetStandardValues(ITypeDescriptorContex
271271
IComparer comparer = Comparer;
272272
if (comparer != null)
273273
{
274-
objValues.Sort((IComparer<object>) comparer);
274+
objValues.Sort(comparer);
275275
}
276276
}
277277

src/System.ComponentModel.TypeConverter/tests/EnumConverterTest.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System.Globalization;
6+
using System.Linq;
67
using Xunit;
78

89
namespace System.ComponentModel.Tests
@@ -97,6 +98,15 @@ public static void ConvertTo_WithContext_Negative()
9798
() => new EnumConverter(typeof(Enum)).ConvertTo(TypeConverterTests.s_context, null, SomeFlagsEnum.Option1, typeof(string)));
9899
}
99100

101+
[Fact]
102+
public static void GetStandardValues_Succeeds()
103+
{
104+
var converter = new EnumConverter(typeof(SomeEnum));
105+
SomeEnum[] standardValues = converter.GetStandardValues().Cast<SomeEnum>().ToArray();
106+
Assert.Equal(Enum.GetNames(typeof(SomeEnum)).Length, standardValues.Length);
107+
Assert.All(Enum.GetValues(typeof(SomeEnum)).Cast<SomeEnum>(), value => Assert.Contains(value, standardValues));
108+
}
109+
100110
private static void VerifyArraysEqual<T>(T[] expected, object actual)
101111
{
102112
Assert.NotNull(actual);

0 commit comments

Comments
 (0)