How can I implement static methods on an interface?

前端 未结 8 1925
生来不讨喜
生来不讨喜 2020-12-07 21:26

I have a 3rd party C++ DLL that I call from C#.

The methods are static.

I want to abstract it out to do some unit testing so I created an interface with the

8条回答
  •  萌比男神i
    2020-12-07 22:27

    Static members are perfectly legal in the CLR, just not C#.

    You could implement some glue in IL to link up the implementation details.

    Not sure if the C# compiler would allow calling them though?

    See: 8.9.4 Interface type definition ECMA-335.

    Interface types are necessarily incomplete since they say nothing about the representation of the values of the interface type. For this reason, an interface type definition shall not provide field definitions for values of the interface type (i.e., instance fields), although it can declare static fields (see §8.4.3).

    Similarly, an interface type definition shall not provide implementations for any methods on the values of its type. However, an interface type definition can—and usually does—define method contracts (method name and method signature) that shall be implemented by supporting types. An interface type definition can define and implement static methods (see §8.4.3) since static methods are associated with the interface type itself rather than with any value of the type.

提交回复
热议问题