Skip to content

Commit e336090

Browse files
committed
Fixup bad code
1 parent 0199386 commit e336090

File tree

1 file changed

+40
-34
lines changed

1 file changed

+40
-34
lines changed

src/Components/Components/src/Reflection/MemberAssignment.cs

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public static PropertyEnumerable GetPropertiesIncludingInherited(
1717
[DynamicallyAccessedMembers(Component)] Type type,
1818
BindingFlags bindingFlags)
1919
{
20-
var dictionary = new Dictionary<string, OneOrMoreProperties>(StringComparer.Ordinal);
20+
var dictionary = new Dictionary<string, object>(StringComparer.Ordinal);
2121

2222
Type? currentType = type;
2323

@@ -28,12 +28,21 @@ public static PropertyEnumerable GetPropertiesIncludingInherited(
2828
{
2929
if (!dictionary.TryGetValue(property.Name, out var others))
3030
{
31-
others = new OneOrMoreProperties { Single = property };
32-
dictionary.Add(property.Name, others);
31+
dictionary.Add(property.Name, property);
3332
}
3433
else if (!IsInheritedProperty(property, others))
3534
{
36-
others.Add(property);
35+
List<PropertyInfo> many;
36+
if (others is PropertyInfo single)
37+
{
38+
many = new List<PropertyInfo> { single };
39+
dictionary[property.Name] = many;
40+
}
41+
else
42+
{
43+
many = (List<PropertyInfo>)others;
44+
}
45+
many.Add(property);
3746
}
3847
}
3948

@@ -43,15 +52,15 @@ public static PropertyEnumerable GetPropertiesIncludingInherited(
4352
return new PropertyEnumerable(dictionary);
4453
}
4554

46-
private static bool IsInheritedProperty(PropertyInfo property, OneOrMoreProperties others)
55+
private static bool IsInheritedProperty(PropertyInfo property, object others)
4756
{
48-
if (others.Single is not null)
57+
if (others is PropertyInfo single)
4958
{
50-
return others.Single.GetMethod?.GetBaseDefinition() == property.GetMethod?.GetBaseDefinition();
59+
return single.GetMethod?.GetBaseDefinition() == property.GetMethod?.GetBaseDefinition();
5160
}
5261

53-
Debug.Assert(others.Many is not null);
54-
foreach (var other in CollectionsMarshal.AsSpan(others.Many))
62+
var many = (List<PropertyInfo>)others;
63+
foreach (var other in CollectionsMarshal.AsSpan(many))
5564
{
5665
if (other.GetMethod?.GetBaseDefinition() == property.GetMethod?.GetBaseDefinition())
5766
{
@@ -62,28 +71,11 @@ private static bool IsInheritedProperty(PropertyInfo property, OneOrMoreProperti
6271
return false;
6372
}
6473

65-
public struct OneOrMoreProperties
66-
{
67-
public PropertyInfo? Single;
68-
public List<PropertyInfo>? Many;
69-
70-
public void Add(PropertyInfo property)
71-
{
72-
if (Many is null)
73-
{
74-
Many ??= new() { Single! };
75-
Single = null;
76-
}
77-
78-
Many.Add(property);
79-
}
80-
}
81-
8274
public ref struct PropertyEnumerable
8375
{
8476
private readonly PropertyEnumerator _enumerator;
8577

86-
public PropertyEnumerable(Dictionary<string, OneOrMoreProperties> dictionary)
78+
public PropertyEnumerable(Dictionary<string, object> dictionary)
8779
{
8880
_enumerator = new PropertyEnumerator(dictionary);
8981
}
@@ -94,16 +86,27 @@ public PropertyEnumerable(Dictionary<string, OneOrMoreProperties> dictionary)
9486
public ref struct PropertyEnumerator
9587
{
9688
// Do NOT make this readonly, or MoveNext will not work
97-
private Dictionary<string, OneOrMoreProperties>.Enumerator _dictionaryEnumerator;
89+
private Dictionary<string, object>.Enumerator _dictionaryEnumerator;
9890
private Span<PropertyInfo>.Enumerator _spanEnumerator;
9991

100-
public PropertyEnumerator(Dictionary<string, OneOrMoreProperties> dictionary)
92+
public PropertyEnumerator(Dictionary<string, object> dictionary)
10193
{
10294
_dictionaryEnumerator = dictionary.GetEnumerator();
10395
_spanEnumerator = Span<PropertyInfo>.Empty.GetEnumerator();
10496
}
10597

106-
public PropertyInfo Current => _spanEnumerator.Current;
98+
public PropertyInfo Current
99+
{
100+
get
101+
{
102+
if (_dictionaryEnumerator.Current.Value is PropertyInfo property)
103+
{
104+
return property;
105+
}
106+
107+
return _spanEnumerator.Current;
108+
}
109+
}
107110

108111
public bool MoveNext()
109112
{
@@ -118,11 +121,14 @@ public bool MoveNext()
118121
}
119122

120123
var oneOrMoreProperties = _dictionaryEnumerator.Current.Value;
121-
var span = oneOrMoreProperties.Single is { } property ?
122-
MemoryMarshal.CreateSpan(ref property, 1) :
123-
CollectionsMarshal.AsSpan(oneOrMoreProperties.Many);
124+
if (oneOrMoreProperties is PropertyInfo)
125+
{
126+
_spanEnumerator = Span<PropertyInfo>.Empty.GetEnumerator();
127+
return true;
128+
}
124129

125-
_spanEnumerator = span.GetEnumerator();
130+
var many = (List<PropertyInfo>)oneOrMoreProperties;
131+
_spanEnumerator = CollectionsMarshal.AsSpan(many).GetEnumerator();
126132
var moveNext = _spanEnumerator.MoveNext();
127133
Debug.Assert(moveNext, "We expect this to at least have one item.");
128134
return moveNext;

0 commit comments

Comments
 (0)