空接口有什么用
我在看nServiceBus,过来了这个接口,
namespace NServiceBus
{
public interface IMessage
{
}
}
空接口有什么用?
I am looking at nServiceBus and came over this interface
namespace NServiceBus
{
public interface IMessage
{
}
}
What is the use of an empty interface?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
通常它是为了表示类的使用情况。 您可以实现 IMessage 来表明您的类是一条消息。 然后,其他代码可以使用反射来查看您的对象是否旨在用作消息并采取相应的操作。
这是在 Java 出现注释之前就经常使用的东西。 在 .Net 中,使用属性来实现此目的更清晰。
@Stimpy77 谢谢! 我没有那样想过。
我希望您允许我以更笼统的方式重新表述您的评论。
必须在运行时使用反射检查注释和属性。 可以使用编译器中的类型系统在编译时检查空接口。 这在运行时根本不会带来任何开销,因此速度更快。
Usually it's to signal usage of a class. You can implement IMessage to signal that your class is a message. Other code can then use reflection to see if your objects are meant to be used as messages and act accordingly.
This is something that was used in Java a lot before they had annotations. In .Net it's cleaner to use attributes for this.
@Stimpy77 Thanks! I hadn't thought of it that way.
I hope you'll allow me to rephrase your comment in a more general way.
Annotations and attributes have to be checked at runtime using reflection. Empty interfaces can be checked at compile-time using the type-system in the compiler. This brings no overhead at runtime at all so it is faster.
也称为标记接口。
Also known as a Marker Interface.
在 java 中,
Serialized
就是一个完美的例子。 它没有定义任何方法,但“实现”它的每个类都必须确保它确实是可序列化的,并且不包含对无法序列化的事物的引用,例如数据库连接、打开的文件等。In java
Serializable
is the perfect example for this. It defines no methods but every class that "implements" it has to make sure, that it is really serializable and holds no reference to things that cannot be serialized, like database connections, open files etc.在 Java 中,空接口通常用于“标记”类——现在通常使用注释。
这只是向类添加一些元数据的一种方式,表示“该类适合
类型的使用”,即使不涉及公共成员。In Java, empty interfaces were usually used for "tagging" classes - these days annotations would normally be used.
It's just a way of adding a bit of metadata to a class saying, "This class is suitable for
<this>
kind of use" even when no common members will be involved.通常它与属性类似。 使用属性优于空接口(至少 FxCop 知道)。 但是 .NET 本身使用其中一些接口,例如 IRequiresSessionState 和 IReadOnlySessionState。 我认为当您使用使元数据查找使用接口的属性时,元数据查找会出现性能损失。
Normally it's similar to attributes. Using attributes is a preferred to empty interfaces (at least as much as FxCop is aware). However .NET itself uses some of these interfaces like
IRequiresSessionState
andIReadOnlySessionState
. I think there is performance loss in metadata lookup when you use attributes that made them use interfaces instead.空接口仅充当数据类型的占位符,而无需在其接口行为中更好地指定。
在Java中,接口扩展机制就是一个很好的使用例子。 例如,假设我们有以下
接口三将继承“一”和“二”的行为,因此
必须指定这两个方法,其类型为“三”。
正如你所看到的,从上面的例子中,空接口也可以被看作是多重继承的一个点(Java中不允许)。
希望这有助于澄清进一步的观点。
An empty interface acts simply as a placeholder for a data type no better specified in its interface behaviour.
In Java, the mechanism of the interface extension represents a good example of use. For example, let's say that we've the following
Interface three will inherit the behaviour of 'one' and 'two', and so
has to specify the two methods, being of type 'three'.
As you can see, from the above example, empty interface can be seen also as a point of multiple inheritance (not allowed in Java).
Hoping this helps to clarify with a further viewpoint.
它们被称为“标记接口”,旨在向标记类的实例发出信号。
例如...在 C++ 中,将对象标记为“ICollectible”是一种常见做法,以便它们可以存储在通用非类型集合中。
因此,就像有人说的那样,它们是为了表示某些对象支持的行为,例如收集、序列化的能力等。
They're called "Mark Interfaces" and are meant to signal instances of the marked classes.
For example... in C++ is a common practice to mark as "ICollectible" objects so they can be stored in generic non typed collections.
So like someone over says, they're to signal some object supported behavior, like ability to be collected, serialized, etc.
空接口用于记录实现给定接口的类具有某种行为。
例如,在 java 中,Java 中的 Cloneable 接口就是一个空接口。 当一个类实现了
Cloneable
接口时,您就知道可以在其上调用运行clone()
。Empty interfaces are used to document that the classes that implement a given interface have a certain behaviour
For example in java the
Cloneable
interface in Java is an empty interface. When a class implements theCloneable
interface you know that you can call run theclone()
on it.去年一直与NServiceBus合作。 虽然我不会代表 Udi Dahan 发言,但我的理解是该界面确实主要用作标记。
不过我建议你问问那个人本人,他是否考虑过将其留到将来扩展。 我的赌注是否定的,因为口头禅似乎是让消息保持非常简单或至少与平台无关。
其他人很好地回答了空接口的更普遍的原因。
Been working with NServiceBus for the past year. While I wouldn't speak for Udi Dahan my understanding is that this interface is indeed used as a marker primarily.
Though I'd suggest you ask the man himself if he'd had thoughts of leaving this for future extension. My bet is no, as the mantra seems to be to keep messages very simple or at least practically platform agnostic.
Others answer well on the more general reasons for empty interfaces.
我想说它用于“未来”参考或者如果您想共享一些对象,这意味着您可以有 10 个类,每个类都实现此接口。
并将它们发送到一个函数来处理它们,但如果接口是空的,我会说它只是“预”工作。
I'd say its used for "future" reference or if you want to share some objects, meaning you could have 10 classes each implementing this interface.
And have them sent to a function for work on them, but if the interface is empty, I'd say its just "pre"-work.
空接口用于标记类,在运行时可以使用接口执行类型检查。
例如
Java 编程语言中标记接口的一个应用是 Serialized 接口。 类实现此接口以指示其非瞬态数据成员可以写入 ObjectOutputStream。 ObjectOutputStream 私有方法 writeObject() 包含一系列用于确定可写性的 instanceof 测试,其中一个测试寻找 Serialized 接口。 如果这些测试中的任何一个失败,该方法将抛出 NotSerializedException。
Empty interfaces are used to mark the class, at run time type check can be performed using the interfaces.
For example
An application of marker interfaces from the Java programming language is the Serializable interface. A class implements this interface to indicate that its non-transient data members can be written to an ObjectOutputStream. The ObjectOutputStream private method writeObject() contains a series of instanceof tests to determine writeability, one of which looks for the Serializable interface. If any of these tests fails, the method throws a NotSerializableException.
空接口可用于对特定用途下的类进行分类。 (标记接口)
示例:数据库实体
例如,如果您将使用通用存储库(例如IEntityRepository),则使用通用约束,您可以防止开发者发送未实现IEntity接口的类。
An empty interface can be used to classify classes under a specific purpose. (Marker Interface)
Example : Database Entities
For Instance, If you will be using Generic Repository(ex. IEntityRepository), using generic constraints, you can prevent the classes that do not implement the IEntity interface from being sent by the developers.