How can I implement static methods on an interface?

前端 未结 8 1944
生来不讨喜
生来不讨喜 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条回答
  •  情话喂你
    2020-12-07 22:13

    You can't define static members on an interface in C#. An interface is a contract for instances.

    I would recommend creating the interface as you are currently, but without the static keyword. Then create a class StaticIInterface that implements the interface and calls the static C++ methods. To do unit testing, create another class FakeIInterface, that also implements the interface, but does what you need to handle your unit tests.

    Once you have these 2 classes defined, you can create the one you need for your environment, and pass it to MyClass's constructor.

提交回复
热议问题