C# interface static method call with generics

2020-02-16 12:34发布

Is there a simple way to implement this, and if possible without instanciating an object :

interface I
{
     static  string GetClassName();
}

public class Helper
{

    static void PrintClassName<T>() where T : I
    {
         Console.WriteLine(T.GetClassName());
    }
}

7条回答
对你真心纯属浪费
2楼-- · 2020-02-16 13:03

Declaring a static property, event or method on an interface definition is not considered a legal definition. This is because interfaces are considered contracts and as such, represent something that will be implemented by every client instance of that interface.

A static declaration essentially states that the static member does not require a physical client implementation in order to execute the required functionality and this falls short of the general concept of interfaces: providing a proven contract.

查看更多
登录 后发表回答