2

我正在 .fsx 脚本中尝试预编译正则表达式。但我不知道如何为生成的程序集指定 .dll 文件位置。我已经尝试在使用CodeBaseAssemblyName实例上设置属性,Regex.CompileToAssembly但无济于事。这是我所拥有的:

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:\Users\Stephen\Documents\Visual Studio 2010\Projects\Unquote\code\Unquote\Unquote.Regex.dll。如果我在 C 驱动器中搜索Unquote.Regex.dll,我会在某个临时 AppData 文件夹中找到它。

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

4

1 回答 1

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__)
于 2012-04-15T17:01:50.917 回答