Microsoft.Extensions.DependencyInjection can not register type which implement static abstract memer interface

William Liu 306 Reputation points
2024-05-20T02:20:59.1633333+00:00

I can not register the type Foo as 'IFoo' when IFoo has static abstract member. Is it a bug? Or, how can I register this type as interface?

repo

Program.cs

using Microsoft.Extensions.DependencyInjection;

// these line works!
IFoo foo = new Foo();
Foo.GetCount();

var services = new ServiceCollection();
services.AddSingleton<IFoo, Foo>(); // why this line can not compile?

// error message:
// The interface 'IFoo' type cannot be used as a type argument for type parameter 'TService'.
// The interface has static abstract members without implementations: int IFoo.GetCount()

public interface IFoo
{
    static abstract int GetCount();
}

public class Foo : IFoo
{
    public static int GetCount()
    {
        return 1;
    }
}

StaticAbstractMemberInInterfaceIssue.csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
  </ItemGroup>

</Project>

enter image description here

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,482 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiale Xue - MSFT 38,101 Reputation points Microsoft Vendor
    2024-05-20T07:15:58.69+00:00

    Hi @William Liu ,Welcome to Microsoft Q&A,

    In C#, static abstract members in an interface are intended to be used in scenarios involving generic math or other compile-time polymorphism, but they can't be instantiated directly or used in the context of dependency injection.

    You can use it like this:

    using Microsoft.Extensions.DependencyInjection;
    
    public interface IFoo
    {
        // Removed static abstract member
    }
    
    public static class FooUtilities
    {
        public static int GetCount()
        {
            return 1;
        }
    }
    
    public class Foo : IFoo
    {
        // Instance members of Foo
    }
    
    class Program
    {
        static void Main()
        {
            var services = new ServiceCollection();
            services.AddSingleton<IFoo, Foo>();
    
            var serviceProvider = services.BuildServiceProvider();
            IFoo foo = serviceProvider.GetRequiredService<IFoo>();
    
            int count = FooUtilities.GetCount();
            Console.WriteLine($"Count: {count}");
        }
    }
    
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful