public static void IlasmTempAssembly(string declarations, bool appendDefaultHeader, bool includePdb, out string assemblyPath, out string pdbPath) { if (declarations == null) { throw new ArgumentNullException(nameof(declarations)); } using (var sourceFile = new DisposableFile(extension: ".il")) { string sourceFileName = Path.GetFileNameWithoutExtension(sourceFile.Path); assemblyPath = Path.Combine( TempRoot.Root, Path.ChangeExtension(Path.GetFileName(sourceFile.Path), "dll")); string completeIL; if (appendDefaultHeader) { completeIL = string.Format( @".assembly '{0}' {{}} .assembly extern mscorlib {{ .publickeytoken = (B7 7A 5C 56 19 34 E0 89) .ver 4:0:0:0 }} {1}", sourceFileName, declarations); } else { completeIL = declarations.Replace("<<GeneratedFileName>>", sourceFileName); } sourceFile.WriteAllText(completeIL); var ilasmPath = Path.Combine( Path.GetDirectoryName(typeof(object).Assembly.Location), "ilasm.exe"); var arguments = string.Format( "\"{0}\" /DLL /OUT=\"{1}\"", sourceFile.Path, assemblyPath); if (includePdb && !CLRHelpers.IsRunningOnMono()) { pdbPath = Path.ChangeExtension(assemblyPath, "pdb"); arguments += string.Format(" /PDB=\"{0}\"", pdbPath); } else { pdbPath = null; } var program = ilasmPath; if (CLRHelpers.IsRunningOnMono()) { arguments = string.Format("{0} {1}", ilasmPath, arguments); arguments = arguments.Replace("\"", ""); arguments = arguments.Replace("=", ":"); program = "mono"; } var result = ProcessLauncher.Run(program, arguments); if (result.ContainsErrors) { throw new ArgumentException( "The provided IL cannot be compiled." + Environment.NewLine + program + " " + arguments + Environment.NewLine + result, "declarations"); } } }
public static void IlasmTempAssembly(string declarations, bool appendDefaultHeader, bool includePdb, out string assemblyPath, out string pdbPath) { if (declarations == null) { throw new ArgumentNullException(nameof(declarations)); } using (var sourceFile = new DisposableFile(extension: ".il")) { string sourceFileName = Path.GetFileNameWithoutExtension(sourceFile.Path); assemblyPath = Path.Combine( TempRoot.Root, Path.ChangeExtension(Path.GetFileName(sourceFile.Path), "dll")); string completeIL; if (appendDefaultHeader) { const string corLibName = "mscorlib"; const string corLibVersion = "4:0:0:0"; const string corLibKey = "B7 7A 5C 56 19 34 E0 89"; completeIL = $@".assembly '{sourceFileName}' {{}} .assembly extern {corLibName} {{ .publickeytoken = ({corLibKey}) .ver {corLibVersion} }} {declarations}"; } else { completeIL = declarations.Replace("<<GeneratedFileName>>", sourceFileName); } sourceFile.WriteAllText(completeIL); var arguments = $"\"{sourceFile.Path}\" -DLL -out=\"{assemblyPath}\""; if (includePdb && !MonoHelpers.IsRunningOnMono()) { pdbPath = Path.ChangeExtension(assemblyPath, "pdb"); arguments += string.Format(" -PDB=\"{0}\"", pdbPath); } else { pdbPath = null; } var result = ProcessUtilities.Run(IlasmPath, arguments); if (result.ContainsErrors) { throw new ArgumentException( "The provided IL cannot be compiled." + Environment.NewLine + IlasmPath + " " + arguments + Environment.NewLine + result, nameof(declarations)); } } }
public static void IlasmTempAssembly(string declarations, bool appendDefaultHeader, bool includePdb, out string assemblyPath, out string pdbPath) { if (declarations == null) throw new ArgumentNullException(nameof(declarations)); using (var sourceFile = new DisposableFile(extension: ".il")) { string sourceFileName = Path.GetFileNameWithoutExtension(sourceFile.Path); assemblyPath = Path.Combine( TempRoot.Root, Path.ChangeExtension(Path.GetFileName(sourceFile.Path), "dll")); string completeIL; if (appendDefaultHeader) { completeIL = string.Format( @".assembly '{0}' {{}} .assembly extern mscorlib {{ .publickeytoken = (B7 7A 5C 56 19 34 E0 89) .ver 4:0:0:0 }} {1}", sourceFileName, declarations); } else { completeIL = declarations.Replace("<<GeneratedFileName>>", sourceFileName); } sourceFile.WriteAllText(completeIL); var ilasmPath = Path.Combine( Path.GetDirectoryName(typeof(object).Assembly.Location), "ilasm.exe"); var arguments = string.Format( "\"{0}\" /DLL /OUT=\"{1}\"", sourceFile.Path, assemblyPath); if (includePdb && !CLRHelpers.IsRunningOnMono()) { pdbPath = Path.ChangeExtension(assemblyPath, "pdb"); arguments += string.Format(" /PDB=\"{0}\"", pdbPath); } else { pdbPath = null; } var program = ilasmPath; if (CLRHelpers.IsRunningOnMono()) { arguments = string.Format("{0} {1}", ilasmPath, arguments); arguments = arguments.Replace("\"", ""); arguments = arguments.Replace("=", ":"); program = "mono"; } var result = ProcessLauncher.Run(program, arguments); if (result.ContainsErrors) { throw new ArgumentException( "The provided IL cannot be compiled." + Environment.NewLine + program + " " + arguments + Environment.NewLine + result, "declarations"); } } }