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

查看:102
本文介绍了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.

这是一个巨大的hack,但是似乎有些人可能会发现它很有用,所以我将其分享.

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天全站免登陆