I’m experimenting with precompile regexes in an .fsx script. But I can’t figure out how to specify the .dll file location for the generated assembly. I’ve tried setting properties such as CodeBase on the AssemblyName instance used by Regex.CompileToAssembly but to no avail. Here’s what I have:
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)
I’m executing this in FSI and when I evaluate an I see:
> 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;}
But, again, I don’t see C:\Users\Stephen\Documents\Visual Studio 2010\Projects\Unquote\code\Unquote\Unquote.Regex.dll like I want. If I search my C-drive for Unquote.Regex.dll, I do find it in some temp AppData folder.
So, how can I correctly specify the .dll file location for an assembly generated by Regex.CompileToAssembly?
It seems that CompileToAssembly doesn’t respect CodeBase or any other property in AssemblyName and instead just saves result assembly to the current directory. Try to set System.Environment.CurrentDirectory to the proper location and revert it back after saving.