8

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

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?

BuddyJoe
  • 69,735
  • 114
  • 291
  • 466

1 Answers1

16

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.

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

Brian
  • 117,631
  • 17
  • 236
  • 300
  • Can a script pick up where it is running from? that would be nice too. – BuddyJoe Jun 23 '10 at 15:56
  • 1
    Note that this methof may get you into trouble if there is more than one instance of VS running on your system. – Dmitry Lomov Jun 23 '10 at 19:49
  • that is a giant hack. but nice to get into the inner-workings sometimes. – BuddyJoe Jun 23 '10 at 20:38
  • Hi Brian, I hope you saw this is a recurring question (http://stackoverflow.com/questions/9998144/fsx-script-referencing-a-dll-referencing-many-dll) I think it'd make sense to iron it out – nicolas Aug 04 '12 at 13:58
  • I also have a handy hack around that : https://github.com/nrolland/FStat/blob/master/FStat/scriptSetup.fsx – nicolas Aug 04 '12 at 14:00