简体   繁体   English

F#/ C#-fsx脚本文件和项目参考

[英]F#/C# - fsx Script Files and Project References

You have a solution with one C# project in it. 您有一个包含一个C#项目的解决方案。 SomeComp.Framework is the name. 名称是SomeComp.Framework。 You add a F# project to the solution. 您将F#项目添加到解决方案。 You reference the SomeComp.Framework project in the F# project. 您在F#项目中引用SomeComp.Framework项目。 You insert a script file - test.fsx into the F# project. 您将脚本文件-test.fsx插入F#项目。 What is the correct way to reference the C# assembly in the script file? 在脚本文件中引用C#程序集的正确方法是什么?

#r "SomeComp.Framework.dll" // doesn't work

Is there some way to do this without hardcoding a path? 有某种方法可以不对路径进行硬编码吗? Does the .fsx file get any settings/properties from being located inside a F# project? .fsx文件是否从F#项目中获得任何设置/属性?

No, it does not get any settings from the project, you have to hardcode the path. 不,它没有从项目中获取任何设置,您必须对路径进行硬编码。 (This is a scenario we'll look at improving for a future release.) Be careful about 'Debug' versus 'Release' paths, too. (在以后的发行版中,我们将考虑改进这种情况。)也要注意“调试”路径与“发布”路径。

EDIT 编辑

Ok, wow, I figured out something useful: 好的,哇,我发现了一些有用的东西:

#r "EnvDTE"
#r "EnvDTE80"
#r "VSLangProj"

let appObj = System.Runtime.InteropServices.Marshal.
               GetActiveObject("VisualStudio.DTE") :?> EnvDTE80.DTE2
let solnDir = System.IO.Path.GetDirectoryName(appObj.Solution.FileName)
let cfg = appObj.Solution.SolutionBuild.ActiveConfiguration.Name
let libraryDLLPath = System.IO.Path.Combine 
                       [| solnDir; "Library1"; "bin"; cfg |]

//#r libraryDLL  // illegal, since #r takes a string literal, but...

let props = appObj.Properties("F# Tools", "F# Interactive")
let cla = props.Item("FsiCommandLineArgs")
cla.Value <- sprintf "--optimize -I:\"%s\"" libraryDLLPath

appObj.ExecuteCommand("View.F#Interactive", "")
appObj.ExecuteCommand("OtherContextMenus.FSIConsoleContext.ResetSession", "")

#r "Library1.dll"

Execute that as two snippets, first everything but the last line, and finally the last line. 将其作为两个片段执行,首先执行除最后一行以外的所有内容,最后执行最后一行。 It basically changes your FSI settings inside VS and resets the session, so subsequently you can just #r "MyLibrary.dll" without a path. 它基本上会在VS中更改您的FSI设置并重置会话,因此随后您只需#r "MyLibrary.dll"无需路径。

It is a giant hack, but it seems like some folks may find it useful, so I am sharing it. 这是一个巨大的骇客,但似乎有些人可能会发现它很有用,所以我将其分享。

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

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