2012-04-15 8 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私が望むように。私がのUnquote.Regex.dllのCドライブを検索すると、私はそれをいくつかの一時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

ありがとうございます! –

関連する問題