C# 如何在接口上实现静态方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9415257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I implement static methods on an interface?
提问by Jon
I have a 3rd party C++ DLL that I call from C#.
我有一个从 C# 调用的第 3 方 C++ DLL。
The methods are static.
方法是静态的。
I want to abstract it out to do some unit testing so I created an interface with the static methods in it but now my program errors with:
我想把它抽象出来做一些单元测试,所以我创建了一个包含静态方法的接口,但现在我的程序错误:
The modifier 'static' is not valid for this item
修饰符 'static' 对此项无效
MyMethod cannot be accessed with an instance reference; qualify it with a type name instead
How can I achieve this abstraction?
我怎样才能实现这种抽象?
My code looks like this
我的代码看起来像这样
private IInterfaceWithStaticMethods MyInterface;
public MyClass(IInterfaceWithStaticMethods myInterface)
{
this.MyInterface = myInterface;
}
public void MyMethod()
{
MyInterface.StaticMethod();
}
采纳答案by davisoa
You can't define static members on an interface in C#. An interface is a contract for instances.
您不能在 C# 中的接口上定义静态成员。接口是实例的契约。
I would recommend creating the interface as you are currently, but without the static keyword. Then create a class StaticIInterfacethat 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.
我建议您按照当前的方式创建界面,但不要使用 static 关键字。然后创建一个StaticIInterface实现接口并调用静态 C++ 方法的类。要进行单元测试,请创建另一个类FakeIInterface,该类也实现了接口,但会执行处理单元测试所需的操作。
Once you have these 2 classes defined, you can create the one you need for your environment, and pass it to MyClass's constructor.
一旦定义了这 2 个类,就可以创建环境所需的类,并将其传递给MyClass的构造函数。
回答by Danny Varod
Interfaces can't have static members and static methods can not be used as implementation of interface methods.
接口不能有静态成员,静态方法不能用作接口方法的实现。
What you can do is use an explicit interface implementation:
您可以做的是使用显式接口实现:
public interface IMyInterface
{
void MyMethod();
}
public class MyClass : IMyInterface
{
static void MyMethod()
{
}
void IMyInterface.MyMethod()
{
MyClass.MyMethod();
}
}
Alternatively, you could simply use non-static methods, even if they do not access any instance specific members.
或者,您可以简单地使用非静态方法,即使它们不访问任何特定于实例的成员。
回答by Justin Pihony
As to why you cannot have a static method on an interface: Why Doesn't C# Allow Static Methods to Implement an Interface?
至于为什么不能在接口上使用静态方法:为什么 C# 不允许静态方法实现接口?
However, I would suggest removing the static methods in favor of instance methods. If that is not possible, then you could wrap the static method calls inside of an instance method, and then you can create an interface for that and run your unit tests from that.
但是,我建议删除静态方法以支持实例方法。如果这是不可能的,那么您可以将静态方法调用包装在一个实例方法中,然后您可以为此创建一个接口并从中运行单元测试。
ie
IE
public static class MyStaticClass
{
public static void MyStaticMethod()
{...}
}
public interface IStaticWrapper
{
void MyMethod();
}
public class MyClass : IStaticWrapper
{
public void MyMethod()
{
MyStaticClass.MyStaticMethod();
}
}
回答by John Koerner
You could invoke it with reflection:
您可以通过反射调用它:
MyInterface.GetType().InvokeMember("StaticMethod", BindingFlags.Static | BindingFlags.Public | BindingFlags.InvokeMethod, null, null, null);
回答by leppie
Static members are perfectly legal in the CLR, just not C#.
静态成员在 CLR 中是完全合法的,只是在 C# 中不合法。
You could implement some glue in IL to link up the implementation details.
您可以在 IL 中实现一些胶水来链接实现细节。
Not sure if the C# compiler would allow calling them though?
不确定 C# 编译器是否允许调用它们?
See: 8.9.4 Interface type definition ECMA-335.
参见:8.9.4 接口类型定义 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.
接口类型必然是不完整的,因为它们没有说明接口类型的值的表示。为此,接口类型定义不应为接口类型的值(即实例字段)提供字段定义,尽管它可以声明静态字段(参见第 8.4.3 节)。
同样,接口类型定义不应为其类型的值提供任何方法的实现。然而,接口类型定义可以——而且通常确实——定义应由支持类型实现的方法契约(方法名称和方法签名)。接口类型定义可以定义和实现静态方法(参见第 8.4.3 节),因为静态方法与接口类型本身相关联,而不是与该类型的任何值相关联。
回答by AliReza
You can define static methods in c# 8 but you must declare a default body for it.
您可以在 c# 8 中定义静态方法,但必须为其声明默认主体。
public interface IMyInterface
{
static string GetHello() => "Default Hello from interface" ;
static void WriteWorld() => Console.WriteLine("Writing World from interface");
}
or if you don't want to have any default body simply throw an exception:
或者如果您不想拥有任何默认主体,只需抛出异常:
public interface IMyInterface
{
static string GetHello() => throw new NotImplementedException() ;
static void WriteWorld() => throw new NotImplementedException();
}

