简体   繁体   English

在 C# 中向其他人提供接口定义的最佳方式是什么?

[英]What's the best way to provide an interface definition to others in C#?

This strikes me as likely to have an agreed best-practice answer, but I can't seem to find one anywhere.这让我觉得很可能有一个公认的最佳实践答案,但我似乎无法在任何地方找到答案。

I have an application that will load and use classes that implement a specific interface.我有一个应用程序,它将加载和使用实现特定接口的类。 The implementation classes will be written by someone else and I want to send them the minimum they need to successfully implement the interface.实现类将由其他人编写,我想向他们发送成功实现接口所需的最低限度。

So far the best I've come up with is the following:到目前为止,我想出的最好的方法如下:

The application solution that contains: A project that contains just the interface definition and compiles to a dll.包含以下内容的应用程序解决方案: 仅包含接口定义并编译为 dll 的项目。 A project for the application that uses the interface and references the dll.使用该接口并引用 dll 的应用程序项目。

A separate solution for an example implementation that builds to a dll and references the interface dll.构建到 dll 并引用接口 dll 的示例实现的单独解决方案。

Is this the best way to do this?这是最好的方法吗? ie distribute a compiled version of the interface to anyone that needs to implement the interface.即将接口的编译版本分发给需要实现接口的任何人。

I tried using just a copy of the interface source files in the example implementation and my application failed to recognise the class as implementing the interface.我尝试在示例实现中仅使用接口源文件的副本,但我的应用程序无法将 class 识别为实现接口。 Is this to be expected or is my class loading code bugged (it does work when the example references the pre-compiled dll)?这是意料之中的还是我的 class 加载代码有问题(当示例引用预编译的 dll 时它确实有效)?

you should put your interface in an assembly and then distribute your assembly (or your whole project if needed) so that the other people who want to implement the interface just need to reference your assembly so they have access to the same interface (which is not the case if you just send the interface (.cs) file as your interface will be embedded in another assembly and thus will certainly have another namespace or assembly name and thats why your implementation class was not recognized as inheriting your interface cause basically it was not the same interface even if the methods and properties where the same;))您应该将您的接口放在一个程序集中,然后分发您的程序集(或您的整个项目,如果需要),以便其他想要实现该接口的人只需要引用您的程序集,这样他们就可以访问相同的接口(这不是如果您只是发送接口(.cs)文件,因为您的接口将嵌入另一个程序集中,因此肯定会有另一个命名空间或程序集名称,这就是为什么您的实现 class 未被识别为继承您的接口的原因基本上它不是即使方法和属性相同,接口也相同;))

i think your approach first is the best if you dont want people to change your code and just use the interface如果您不希望人们更改您的代码而只使用界面,我认为您的方法首先是最好的

otherwise just share the whole project containing the interface否则只共享包含接口的整个项目

That's the approach I've used, and seen in other projects - give the shared assembly a generic name such as MyApp.Interfaces, in case you end up with multiple shared interfaces.这就是我使用过的方法,并且在其他项目中也看到过——给共享程序集一个通用名称,例如 MyApp.Interfaces,以防您最终得到多个共享接口。

An alternative approach is to use the Managed Extensibility Framework: http://msdn.microsoft.com/en-us/library/dd460648.aspx - but that may be overkill for a small project.另一种方法是使用托管可扩展性框架: http://msdn.microsoft.com/en-us/library/dd460648.aspx - 但这对于小型项目来说可能是多余的。

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

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