问题描述
这就是我的情况。我在C#中有一个类库。它公开了一些类型,例如 Rectangle
。
So here's my situation. I have a class library in C#. It exposes some types like Rectangle
. It also exposes some methods for doing algorithmic computation.
用F#编写这些算法位似乎是一个好主意。因此,我在解决方案中添加了另一个F#库项目。
It seemed like a good idea to write those algorithmic bits in F#. So I added another F# library project to the solution.
现在是问题所在。为了使F#项目执行算法计算,需要了解C#项目中的 Rectangle
。为了使C#项目能够使用F#函数,它需要引用F#项目。因此,有一个循环引用。
Now here's the issue. In order for the F# project to do algorithmic computations it needs to know about the Rectangle
from the C# project. And in order for the C# project to be able to use the F# functions it needs to reference the F# project. So there's the circular reference.
一个可能的解决方案是拥有第三个项目,我可以在其中定义 Rectangle
,然后从F#和C#引用该项目,然后从C#引用F#项目,这样我就可以使用这些函数。
One possible solution is to have a third project where I can define the Rectangle
, then reference that project from both F# and C#, and then reference the F# project from C# so I can use the functions.
问题在于,这样的用户我的库不仅要引用我的C#项目,还要引用其他第三个项目(因为使用需要向算法提供输入)。
The issue with this is that then the user of my library would have to not only reference my C# project but also the extra third project (because the use needs to supply the input to the algorithm).
真的可以接受,因为使用F#只是实现细节,我不希望它在使用库的方式上有任何开销。
This isn't really acceptable since using F# is just an implementation detail and I don't want it to have any overhead on the way my library is used.
所以我们说矩形
保留在C#项目中。算法的F#实现( C#中的方法调用F#中的函数)的接口也位于C#项目中。我如何将它们全部链接起来以便工作?甚至还有其他选择吗?
So let's say the Rectangle
stays in the C# project. The "interface" to the F# implementation (the method in C# that calls the function in F#) of the algorithm is also in the C# project. How do I link it all up so it works? Are there any other options even?
拒绝循环引用对我来说似乎很奇怪。像这样的紧密绑定的项目实际上并没有一个有向图结构。
It seems odd to me to refuse circular references. Tightly bound projects like this one don't really have a directed graph structure.
我知道一种额外的方法。具有自己的 Rectangle
的F#项目,然后在两端进行序列化和反序列化。这既乏味又与使用F#使我的代码更简单的初衷相反。
There's an extra method I'm aware of. The F# project having it's own Rectangle
and then do serialization and deserialization on both ends. This is both tedious and contrary to the original idea of using F# to make my code simpler.
推荐答案
听起来像是您的真正问题不是依赖关系本身,而是有关打包的问题(必须运送三个程序集,并且要求最终用户链接到多个程序集。)在这些情况下,您可以考虑使用将程序集组合到单个DLL中。这使您可以在项目中使用适当的解耦,同时仍可以方便地为库仅运送单个文件。这是第三方库供应商非常常用的策略。
It sounds like your real issue is not the dependency itself, but rather concern over the 'packaging' (having to ship three assemblies and requiring linkage to more than one assembly by the end-user.) In these scenarios, you might consider using ILMerge to combine your assemblies into a single DLL. This allows you to use proper decoupling in your projects while still having the convenience of shipping only a single file for your library. This is a very commonly used strategy by vendors of third-party libraries.
也有各种商业工具可以执行此过程并进行混淆,许可等其他操作。强制执行等等。但是尚不清楚这对于您的应用程序是否确实必要。
There are also various commercial tools available that can perform this process and do other things like obfuscation, license enforcement, etc. but it's unclear whether that's really necessary for your application.
这篇关于解决同时使用C#和F#的项目的循环引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!