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.