【问题标题】:Regex.CompileToAssembly how to set .dll file locationRegex.CompileToAssembly 如何设置 .dll 文件位置
【发布时间】:2012-04-15 16:51:28
【问题描述】:

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

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 文件夹中找到它。

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

【问题讨论】:

    标签: .net regex f#


    【解决方案1】:

    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__)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-27
      • 2017-04-22
      • 2017-11-09
      • 1970-01-01
      • 1970-01-01
      • 2020-04-19
      • 2012-09-28
      • 1970-01-01
      相关资源
      最近更新 更多