简体   繁体   English

不能将静态方法指定为接口的一部分?

[英]Can't specify static methods as part of an Interface?

I have a set of objects that I want to conform to an interface, say ISpecialObject. 我有一组要符合接口的对象,例如ISpecialObject。 However a part of my implementation I want to encapsulate the instantiation trigger of these specialobjects within the implementation of each ISpecialObject. 但是,在实现的一部分中,我想将这些特殊对象的实例化触发器封装在每个ISpecialObject的实现中。

So say for instance I have as list of class types that implement ISpecialObject, I then want to go through each one and call a static method like CanCreate(some data) which tells me whether or not to create an instance of one of these. 举例来说,举例来说,我作为实现ISpecialObject的类类型的列表,然后我想遍历每个类,并调用诸如CanCreate(某些数据)之类的静态方法,该方法告诉我是否创建其中之一的实例。

However, .net doesn't seem to let me specify this static CanCreate as part of the ISpecialObject interface. 但是,.net似乎没有让我将此静态CanCreate指定为ISpecialObject接口的一部分。

Can anyone suggest a way to get around this, or alternatively a better approach to solving the problem of encapsulation of the instantiation of these objects? 任何人都可以提出解决此问题的方法,或者是解决这些对象实例化封装问题的更好方法吗? I may just be thinking about this all wrong. 我可能只是在想这一切错。

Thanks. 谢谢。

Edit: I may have phrased some parts of this poorly. 编辑:我可能对此措辞不佳。 I don't want to provide the implementation in the interface, but rather specify that there will be one, and that it will be static. 我不想在接口中提供实现,而是指定将有一个实现,并且它将是静态的。 Essentially I want the objects to be self defining by allowing a higher level object to query when to create them at runtime. 本质上,我希望通过允许更高级别的对象查询何时在运行时创建它们来自我定义对象。

From what I understand, your main issue is the instantiation of a set of objects that conform to the same interface. 据我了解,您的主要问题是实例化符合同一接口的一组对象。 If that is so, you may want to look at the Factory Design Pattern which is the standard way to encapsulate such logic. 如果是这样,则您可能需要查看“ 工厂设计模式” ,这是封装此类逻辑的标准方法。

.NET does not allow static method declarations on interfaces. .NET不允许在接口上进行静态方法声明。 They don't really make sense since interfaces are all about the contract and avoid implementation entirely. 它们实际上没有任何意义,因为接口都是关于合同的,并且完全避免了实现。 Static methods are specifically about implementation. 静态方法专门用于实现。 Additionally, interface methods are virtual function calls depending on the type of the instance, whereas static methods are independent of an instance or even a class (they could be put on any concrete type). 另外,接口方法是虚拟函数调用,具体取决于实例的类型,而静态方法则独立于实例甚至类(可以将其置于任何具体类型上)。

If you have many implementations of ISpecialObject , you could use a factory pattern. 如果您有ISpecialObject许多实现, ISpecialObject可以使用工厂模式。 In order to do this, you would define define an interface called ISpecialObjectFactory alongside ISpecialObject : 为了做到这一点,你就下定义称为接口ISpecialObjectFactory旁边ISpecialObject

class ISpecialObjectFactory
{
    ISpecialObject CreateInstance(...);
    bool CanCreate(...);
}

Each class that implements ISpecialObject should have a corresponding ISpecialObjectFactory (eg UserObject would have also have a UserObjectFactory ). 每个实现ISpecialObject类都应具有一个对应的ISpecialObjectFactory (例如, UserObject也将具有UserObjectFactory )。 This would require a bit more code, but it's a common pattern and I believe it solves your problem. 这将需要更多代码,但这是一种常见模式,我相信它可以解决您的问题。

I dont see the issue. 我看不到问题。 The typename is just a prefix when dealing with static methods. 在处理静态方法时,类型名只是前缀。 It will make no difference what so ever if the static method lives somewhere else. 如果静态方法位于其他地方,则不会有任何区别。

That said, look at extension methods, which may do want you really want it to :) 就是说,看看扩展方法,它可能确实希望您真的想要它:)

Edit: Another option might be using attributes. 编辑:另一个选项可能正在使用属性。

We just discussed something very similiar to this on another thread. 我们只是在另一个线程上讨论了与此类似的东西。 Extension methods are definitely a way to solve this problem. 扩展方法绝对是解决此问题的一种方法。 They can provide an implementation for an interface, and the methods can be treated as static or used as a method on an instance of an object which is being extended. 它们可以提供接口的实现,并且这些方法可以视为静态方法,也可以用作要扩展的对象实例的方法。

It is not exactly a duplicate in the way that you've phrased the question, but it is duplicate in nature so check out the link below. 它并不是您所说问题的方式完全相同,但是本质上是重复的,因此请查看下面的链接。

StackOverflow - subclass-needs-to-implement-interface-property-as-static StackOverflow-子类需要实现接口属性为静态

Maybe you can use an abstract class as super class for your purpose. 也许您可以出于自己的目的使用抽象类作为超类。 So the static methods go in the abstract class and all derived classes have that as well. 因此,静态方法进入抽象类,所有派生类也都具有该方法。 However, I agree to the the posts above that may be using the factory pattern is a better approach here. 但是,我同意以上可能使用工厂模式的帖子是一种更好的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM