F#/C# - fsx 脚本文件和项目参考 [英] F#/C# - fsx Script Files and Project References

查看:20
本文介绍了F#/C# - fsx 脚本文件和项目参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您有一个包含一个 C# 项目的解决方案.SomeComp.Framework 就是这个名字.您将 F# 项目添加到解决方案中.您在 F# 项目中引用 SomeComp.Framework 项目.您将脚本文件 - test.fsx 插入到 F# 项目中.在脚本文件中引用 C# 程序集的正确方法是什么?

You have a solution with one C# project in it. SomeComp.Framework is the name. You add a F# project to the solution. You reference the SomeComp.Framework project in the F# project. You insert a script file - test.fsx into the F# project. What is the correct way to reference the C# assembly in the script file?

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

有没有办法在不硬编码路径的情况下做到这一点?.fsx 文件是否从位于 F# 项目中获得任何设置/属性?

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?

推荐答案

不,它不会从项目中获取任何设置,您必须对路径进行硬编码.(这是我们将在未来版本中改进的场景.)也要注意调试"与发布"路径.

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.

编辑

好的,哇,我发现了一些有用的东西:

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"

将其作为两个片段执行,首先是除最后一行之外的所有内容,最后是最后一行.它基本上会更改 VS 中的 FSI 设置并重置会话,因此随后您可以在没有路径的情况下 #r "MyLibrary.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.

这是一个巨大的黑客,但似乎有些人可能会觉得它有用,所以我分享它.

It is a giant hack, but it seems like some folks may find it useful, so I am sharing it.

这篇关于F#/C# - fsx 脚本文件和项目参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆