无法在Fsharp脚本文件中打开名称空间 [英] Cannot open namespace in Fsharp script file
问题描述
在SomeLib.fs文件中的单独的Fsharp项目中,将编译以下代码:
When in a separate Fsharp project in a SomeLib.fs file the following code is compiled:
namespace SomeNameSpace
type SomeType =
member this.SomeMember = "Some member"
,您想在脚本文件中引用并使用此类型,例如:
and you want to reference and use this type in a script file like:
#I @"c:/pathToDll/"
#r "SomeLib.dll"
这是不可能的,尽管dll的路径正确并且我检查了所有内容.同样,当SomeLib.fs文件位于同一项目中并由#load引用时,您仍然无法打开名称空间.
This is not possible, although the path to the dll is correct and I checked everything. Also when the SomeLib.fs file is in the same project and referenced by #load, you still cannot open the namespace.
我知道您可以将类型放入模块中,但是我不能这样做,因为该类型已用作Wcf服务类型.
I know you can put the type in a module, but I cannot do this as the type has te be used as a Wcf service type.
推荐答案
经过大量的实验工作和令人惊讶的互联网或F#书籍中的少量信息,我发现了以下内容:
After a lot of experimental work and surprisingly little info on the internet or in F# books I found out the following:
// Cannot use a relative path
//#I @"bin\Debug"
// Have to use a absolute path
#I @"C:\Development\FSharpNameSpaceTest\SomeCSharpLib\bin\Debug"
// But I can reference a Csharp dll lib
#r "SomeCSharpLib.dll"
// I cannot add a reference to an external F# library dll
// #I @"C:\Development\FSharpNameSpaceTest\NameSpace\bin\Debug"
// #r "NameSpace.dll"
// If I directly load the external fs file, it works"
#load @"C:\Development\FSharpNameSpaceTest\NameSpace\SomeNameSpace.fs"
#load "Library1.fs"
// Namespaces in both the local and the external fs files can only be openend if every single file is loaded instead of referencing the dll.
// Referencing a C# dll is no problem
open FSharpNameSpaceTest
open SomeCSharpLib
open NameSpace
我不知道这是否是最佳方法,但是它可以工作.我要做的是,我将为每个项目创建一个fsx文件,并将该fsx文件加载到该项目中,然后再将该fsx文件加载到引用该项目的fsx文件中.
I do not know if this is the most optimal approach but it works. What I will do is, I will create a fsx file for every project that loads the individual fs files in that project and then I will load that fsx file in the fsx file that references the project.
我仍然觉得这一切都很令人困惑和违反直觉.但这可能是我对F#内部作品的有限了解
I still find this all very confusing and counterintuitive. But that might be my limited knowledge of the innerworks of F#
正确而完整的答案是,我没有实现默认的构造函数.不过,如果您不想这样做,可以使用上述方法.感谢Marc Sigrit.
And the right and complete answer is, I didn't implement a default constructor. Still, if you do not wan't to do this, the above approach is an alternative. Thanks to Marc Sigrit.
这篇关于无法在Fsharp脚本文件中打开名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!