C# 为什么静态类不能实现接口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1266422/
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
Why static classes cant implement interfaces?
提问by Boris Callens
Possible Duplicate:
Why Doesn’t C# Allow Static Methods to Implement an Interface?
可能的重复:
为什么 C# 不允许静态方法实现接口?
In my application I want to use a Repository that will do the raw data access (TestRepository
, SqlRepository
, FlatFileRepository
etc).
Because such a repository would be used throughout the runtime of my application it seemed like a sensible thing to me to make it a static class so I could go
在我的应用程序要使用存储库,将做原始数据访问(TestRepository
,SqlRepository
,FlatFileRepository
等)。因为这样的存储库将在我的应用程序的整个运行时使用,所以将其设为静态类对我来说似乎是明智之举,这样我就可以继续了
SqlRepository.GetTheThingById(5);
without it having to be regenerated all the time.
Because I want my repositories to be interchangeable, I want them to implement a common interface: IRepository
.
But when I try to do so, I get:
无需一直再生。因为我希望我的存储库可以互换,所以我希望它们实现一个通用接口:IRepository
. 但是当我尝试这样做时,我得到:
Static classes cannot implement interfaces
静态类不能实现接口
Why can't they? How do you suggest I change my design then? Is there a pattern I could use?
他们为什么不能?那么你建议我如何改变我的设计?有我可以使用的模式吗?
UPDATE
Five years later: this question is visited 20k+ times, I learned about the disadvantages of the repository pattern, learned about IoC and realise my question was poorly formulated.
更新
五年后:这个问题被访问了 20k+ 次,我了解了存储库模式的缺点,了解了 IoC 并意识到我的问题表述得很差。
I wasn't really asking what the C# specification of an interface is, rather why it was deliberately restricting me in this specific way.
我并不是真的在问接口的 C# 规范是什么,而是为什么它故意以这种特定方式限制我。
The practical answer is that the syntax for calling a method on an instance or on a type are different. But the question is closed.
实际的答案是在实例或类型上调用方法的语法是不同的。但问题已经结束。
回答by Joe White
Interfaces can't have static methods. A class that implements an interface needs to implement them all as instance methods. Static classes can't have instance methods. QED.
接口不能有静态方法。实现接口的类需要将它们全部实现为实例方法。静态类不能有实例方法。QED。
回答by n8wrl
Maybe our experience will help. Rather than SqlRepository as a static class, we use AutoFac for injection and hide the container behind a static class. Then each entity has a static repository property:
也许我们的经验会有所帮助。我们没有将 SqlRepository 作为静态类,而是使用 AutoFac 进行注入并将容器隐藏在静态类后面。然后每个实体都有一个静态存储库属性:
public class Part : inheritence...
{
public static IPartRepository Repository
{
get { return IoCContainer.GetInstance<IRepository<Part>>(); }
}
// ... more part-y stuff
}
This way we can swap out the implementation and callers always know where to get it:
这样我们就可以换出实现,调用者总是知道从哪里得到它:
Part p = Part.Repository.Get(id);
In another project there is a PartRepository registered with the container:
在另一个项目中,有一个在容器中注册的 PartRepository:
public class PartRepository : IPartRepository
{
// IPartRepository implementation that talks to injected DAL
}
In yet another project we have mocks for testing, including repositories pre-loaded with known entires:
在另一个项目中,我们有用于测试的模拟,包括预加载已知整体的存储库:
public class MockPartRepository : Dictionary<Part, int>, IPartRepository
{
// IPartRepository implementation based on dictionary
}
...and it is registered with the container for unit testing. The SAME call gets the repository:
...并且它在容器中注册以进行单元测试。SAME 调用获取存储库:
Part p = Part.Repository.Get(id);
回答by JoshJordan
By definition, interfaces create a contract for instances to fulfill. Since you cannot instantiate a static class, static classes cannot implement interfaces.
根据定义,接口为要履行的实例创建契约。由于您无法实例化静态类,因此静态类无法实现接口。
There is no need to have a static repository. Simply make it non-static and instantiate it when you need it.
不需要有静态存储库。只需将其设为非静态并在需要时实例化即可。