编程自学网编程自学网编程自学网

什么是用于的空接口

问题:

I am looking at nServiceBus and came over this interface

我正在看nServiceBus并通过这个界面

namespace NServiceBus
{
    public interface IMessage
    {
    }
}

What is the use of an empty interface?

空接口有什么用?

答案:

#1



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.

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

This is something that was used in Java a lot before they had annotations. In .Net it's cleaner to use attributes for this.

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


未经允许不得转载:编程自学网 » 什么是用于的空接口