runtime: Any way to mask a (broken) extension method?
After upgrading to .NET 7 I’m getting a surprising breaking change from an extension method: System.Collections.Generic.CollectionExtensions.AsReadOnly<T>(System.Collections.Generic.IList<T>)
Any way to mask it so the compiler can’t resolve it?
I investigated going the other way and masking my own .AsReadOnly and found the copy in runtime doesn’t work very well. It’s even documented to not work well. “An object that acts as a read-only wrapper around the current IList<T>”. No. That’s terrible behavior.
About this issue
- Original URL
- State: open
- Created 2 years ago
- Comments: 17 (13 by maintainers)
List<T>.AsReadOnly
is an instance method that usesthis
… it’ll never be aReadOnlyCollection<T>
.The downside is the type check. If we think it’s common to end up invoking this with something that’s already a
ReadOnlyCollection<T>
, then, yeah, it’s worth it, let’s do it. If it’s very rare, not worth it.It’s been a week since .NET 7 shipped, at this point we can’t just change it.
Have you considered renaming your extension method?
I guess their desired behavior would be
ToReadOnly
according to the BCL naming convention.Or, maybe casting to
IReadOnlyList
.