I wonder if I found the right solution how to access static property/method of the interface when the interface is implemented explicitly.
In .NET 7 interface can define static abstract members. For example System.Numerics.INumberBase interface define:
public static abstract TSelf One { get; }
This interface is explicitly implemented by various numeric types, e.g. System.Int32.
/// <inheritdoc cref="INumberBase{TSelf}.One" />
static int INumberBase<int>.One => One;
Now trying to access int.One value.
Here is what I tried:
using System;
public class Program
{
public static void Main()
{
// Does not compile - because One is implemented explicitly
// Compiler: 'int' does not contain a definition for 'One'
Console.WriteLine(int.One);
// Does not compile
// Compiler: A static virtual or abstract interface member can be accessed only on a type parameter.
Console.WriteLine(System.Numerics.INumberBase<int>.One);
// Compiles
Console.WriteLine(GetOne<int>());
}
private static T GetOne<T>() where T : System.Numerics.INumberBase<T> => T.One;
}
Is the GetOne method the only solution (without using reflection) or am I missing something?