Skip to content

Commit 14b9957

Browse files
committed
Minor changes and cleanup
1 parent 78c145e commit 14b9957

1 file changed

Lines changed: 10 additions & 24 deletions

File tree

src/runtime/Types/MpLengthSlot.cs

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@ public static bool CanAssign(Type clrType)
2424
return true;
2525
}
2626

27-
return IsEnumerableWithCount(clrType, out _);
27+
if (typeof(IEnumerable).IsAssignableFrom(clrType))
28+
{
29+
var p = clrType.GetProperty("Count");
30+
return p != null;
31+
}
32+
33+
return false;
2834
}
2935

3036
/// <summary>
@@ -48,9 +54,10 @@ internal static nint impl(BorrowedReference ob)
4854

4955
Type clrType = co.inst.GetType();
5056

51-
// now look for things that implement ICollection<T> directly (non-explicitly)
57+
// now look for things that implement ICollection<T> directly (non-explicitly) or IEnumerable
5258
PropertyInfo p = clrType.GetProperty("Count");
53-
if (p != null && clrType.GetInterfaces().Any(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(ICollection<>)))
59+
if (p != null &&
60+
(clrType.GetInterfaces().Any(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(ICollection<>)) || typeof(IEnumerable).IsAssignableFrom(clrType)))
5461
{
5562
return (int)p.GetValue(co.inst, null);
5663
}
@@ -63,29 +70,8 @@ internal static nint impl(BorrowedReference ob)
6370
return (int)p.GetValue(co.inst, null);
6471
}
6572

66-
// now for IEnumerable implementation
67-
if (IsEnumerableWithCount(clrType, out var countProperty))
68-
{
69-
return (int)countProperty.GetValue(co.inst, null);
70-
}
71-
7273
Exceptions.SetError(Exceptions.TypeError, $"object of type '{clrType.Name}' has no len()");
7374
return -1;
7475
}
75-
76-
private static bool IsEnumerableWithCount(Type clrType, out PropertyInfo countProperty)
77-
{
78-
countProperty = null;
79-
var isEnumerable = typeof(IEnumerable).IsAssignableFrom(clrType) ||
80-
clrType.GetInterfaces().Any(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IEnumerable<>)) ||
81-
clrType.IsInterface && clrType.IsGenericType && clrType.GetGenericTypeDefinition() == typeof(IEnumerable<>);
82-
if (isEnumerable)
83-
{
84-
countProperty = clrType.GetProperty("Count");
85-
return countProperty != null && countProperty.PropertyType == typeof(int);
86-
}
87-
88-
return false;
89-
}
9076
}
9177
}

0 commit comments

Comments
 (0)