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)

Most upvoted comments

Sounds reasonable, although we’d probably need to update the method on List<T> to do the same for consitency.

List<T>.AsReadOnly is an instance method that uses this… it’ll never be a ReadOnlyCollection<T>.

any reason why we might not want to do this?

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 also only a week old.

It’s been a week since .NET 7 shipped, at this point we can’t just change it.

The reason I asked about masking is it’s the most likely fix I’m going to get.

Have you considered renaming your extension method?

… as opposed to…?

I guess their desired behavior would be ToReadOnly according to the BCL naming convention.

Or, maybe casting to IReadOnlyList.