什么是用于的空接口 [英] What is an Empty interface used for

查看:94
本文介绍了什么是用于的空接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看nServiceBus并通过此界面

I am looking at nServiceBus and came over this interface

namespace NServiceBus
{
    public interface IMessage
    {
    }
}

有什么用?一个空的界面?

What is the use of an empty interface?

推荐答案

通常它是用来表示一个类的用法。您可以实现IMessage来表示您的类是消息。然后其他代码可以使用反射来查看您的对象是否要用作消息并相应地执行操作。

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.

这是Java之前使用过的很多东西。注释。在.Net中,使用属性更清晰。

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谢谢!我没想过那样。
我希望你允许我以更一般的方式改写你的评论。

@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.

这篇关于什么是用于的空接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆