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

Commit a4ab230

Browse files
k0st1xstephentoub
authored andcommitted
PropertyDescriptor - MemberDescriptor.Attributes (iteration 2) (#26756)
* PropertyDescriptor - MemberDescriptor.Attributes returns base attribute instead of ancestor's attribute #26600 * PropertyDescriptor - MemberDescriptor.Attributes returns base attribute instead of ancestor's attribute #26600
1 parent ac72f91 commit a4ab230

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

src/System.ComponentModel.TypeConverter/src/System/ComponentModel/MemberDescriptor.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -377,16 +377,20 @@ private void FilterAttributesIfNeeded()
377377
list = new List<Attribute>(_attributes);
378378
}
379379

380-
var set = new HashSet<object>();
380+
var map = new Dictionary<object, int>();
381381

382382
for (int i = 0; i < list.Count;)
383383
{
384-
if (set.Add(list[i].TypeId))
384+
int savedIndex = -1;
385+
object typeId = list[i].TypeId;
386+
if (!map.TryGetValue(typeId, out savedIndex))
385387
{
386-
++i;
388+
map.Add(typeId, i);
389+
i++;
387390
}
388391
else
389392
{
393+
list[savedIndex] = list[i];
390394
list.RemoveAt(i);
391395
}
392396
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,13 @@ public void RemoveSingleAssociation()
160160
Assert.NotEqual(firstAssociatedObject, firstAssociation);
161161
}
162162

163+
[Fact]
164+
public void DerivedPropertyAttribute() {
165+
PropertyDescriptor property = TypeDescriptor.GetProperties(typeof(FooBarDerived))["Value"];
166+
var descriptionAttribute = (DescriptionAttribute)property.Attributes[typeof(DescriptionAttribute)];
167+
Assert.Equal("Derived", descriptionAttribute.Description);
168+
}
169+
163170
private class InvocationRecordingTypeDescriptionProvider : TypeDescriptionProvider
164171
{
165172
public bool ReceivedCall { get; private set; } = false;
@@ -221,6 +228,18 @@ public override bool IsSupportedType(Type type)
221228
}
222229
}
223230

231+
class FooBarBase
232+
{
233+
[Description("Base")]
234+
public virtual int Value { get; set; }
235+
}
236+
237+
class FooBarDerived : FooBarBase
238+
{
239+
[Description("Derived")]
240+
public override int Value { get; set; }
241+
}
242+
224243
private static Tuple<Type, Type>[] s_typesWithConverters =
225244
{
226245
new Tuple<Type, Type> (typeof(bool), typeof(BooleanConverter)),

0 commit comments

Comments
 (0)