Skip to content

ServiceCollectionDescriptorExtensions.Add extension method not bound #36128

@peter-perot

Description

@peter-perot

The compiler does not bind the extension method ServiceCollectionDescriptorExtensions.Add(this IServiceCollection collection, ServiceDescriptor descriptor) when the method is called on an instance of type IServiceCollection. This is the correct compiler behavior, since IServiceCollection derives from IList<ServiceDescriptor>, and IList<> has an interface definition for Add.

On the other hand, when calling Add on an instance of type ServiceCollection (the concrete class, not the interface), the compiler binds the extension method. This is due to the fact that ServiceCollection uses explicit interface method implementation for the Add method.

This is correct so far, but I wonder what is the point for the extension method?

  • It is not bound on IServiceCollection instances.
  • It is bound on ServiceCollection instances.
  • It adds a null-check, which is missing in the implemention of ServiceCollection.

Wouldn't it be better to put the null-check into the class implementation?

An alternative is to rename the extension method to something like AddDescriptor or AddService.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-Extensions-DependencyInjectionbreaking-changeIssue or PR that represents a breaking API or functional change over a previous release.needs-breaking-change-doc-createdBreaking changes need an issue opened with https://github.com/dotnet/docs/issues/new?template=dotnet

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions