2012-04-15 98 views
2

我正在試用.fsx腳本中的預編譯正則表達式。但我不知道如何爲生成的程序集指定.dll文件位置。我已經嘗試在Regex.CompileToAssembly使用的AssemblyName實例上設置CodeBase等屬性,但無濟於事。下面是我有:Regex.CompileToAssembly如何設置.dll文件位置

open System.Text.RegularExpressions 

let rcis = [| 
    new RegexCompilationInfo(
     @"^NumericLiteral([QRZING])$", 
     RegexOptions.None, 
     "NumericLiteral", 
     "Swensen.Unquote.Regex", 
     true 
    ); 
|] 

let an = new System.Reflection.AssemblyName("Unquote.Regex"); 
an.CodeBase <- __SOURCE_DIRECTORY__ + "\\" + "Unquote.Regex.dll" 
Regex.CompileToAssembly(rcis, an) 

我在FSI執行此,當我評價an我看到:

> an;; 
val it : System.Reflection.AssemblyName = 
    Unquote.Regex 
    {CodeBase = "C:\Users\Stephen\Documents\Visual Studio 2010\Projects\Unquote\code\Unquote\Unquote.Regex.dll"; 
    CultureInfo = null; 
    EscapedCodeBase = "C:%5CUsers%5CStephen%5CDocuments%5CVisual%20Studio%202010%5CProjects%5CUnquote%5Ccode%5CUnquote%5CUnquote.Regex.dll"; 
    Flags = None; 
    FullName = "Unquote.Regex"; 
    HashAlgorithm = None; 
    KeyPair = null; 
    Name = "Unquote.Regex"; 
    ProcessorArchitecture = None; 
    Version = null; 
    VersionCompatibility = SameMachine;} 

但同樣,我沒有看到C:\用戶\斯蒂芬\ Documents \ Visual Studio 2010 \ Projects \ Unquote \ code \ Unquote \ Unquote.Regex.dll就像我想要的。如果我搜索我的C驅動器Unquote.Regex.dll,我確實在某個臨時AppData文件夾中找到了它。

那麼,如何正確指定由Regex.CompileToAssembly生成的程序集的.dll文件位置?

回答

4

似乎CompileToAssembly不尊重CodeBase或AssemblyName中的任何其他屬性,而是將結果程序集保存到當前目錄。嘗試將System.Environment.CurrentDirectory設置爲正確的位置,並在保存後將其恢復。

open System.Text.RegularExpressions 

type Regex with 
    static member CompileToAssembly(rcis, an, targetFolder) = 
     let current = System.Environment.CurrentDirectory 
     System.Environment.CurrentDirectory <- targetFolder 
     try 
      Regex.CompileToAssembly(rcis, an) 
     finally 
      System.Environment.CurrentDirectory <- current 


let rcis = [| 
    new RegexCompilationInfo(
     @"^NumericLiteral([QRZING])$", 
     RegexOptions.None, 
     "NumericLiteral", 
     "Swensen.Unquote.Regex", 
     true 
    ); 
|] 

let an = new System.Reflection.AssemblyName("Unquote.Regex"); 
Regex.CompileToAssembly(rcis, an, __SOURCE_DIRECTORY__) 
+0

非常好 - 謝謝! – 2012-04-15 18:26:24