c# - Runtime compilation generates a file that can't be removed -
i have console type thing accepts line of c# code, wraps in surrounding code , compiles assembly. then, invoke method assembly, output result , that's it.
the problem is, assembly needs have name can set friend assembly access non-public classes. named "console".
everything worked expected problem can't run second script after 1 has finished because file named "console" exists in directory , can't overwritten.
i've tried disposing of has dispose method. i've tried manually deleting file file.delete. nothing helped.
so here's code i'm using. hope can me.
csharpcodeprovider provider = new csharpcodeprovider(); var param = new compilerparameters(); param.generateinmemory = false; param.generateexecutable = false; param.outputassembly = "console"; param.referencedassemblies.add("system.dll"); param.referencedassemblies.add("system.core.dll"); param.referencedassemblies.add(assembly.getexecutingassembly().location); var results = provider.compileassemblyfromsource(param, @" using system; using system.collections.generic; using system.linq; using system.text; using system.io; namespace fireflygl { class debugconsole { static void run(stringwriter writer) { var stdout = console.out; console.setout(writer); " + input.text + @" console.setout(stdout); } } }"); if (results.errors.haserrors) { (int = 0; < results.errors.count; ++i) { pushtext(results.errors[i].errortext); } } else { try { var writter = new stringwriter(); results.compiledassembly.gettype("fireflygl.debugconsole").getmethod("run", bindingflags.static | bindingflags.nonpublic).invoke(null, new object[] { writter }); pushtext(writter.tostring()); history.add(input.text); currenthistory = history.count; input.text = ""; writter.dispose(); } catch (exception) { } } provider.dispose(); file.delete(results.compiledassembly.location);
you have unload assembly rid of references. unfortunately, can't that. can unload appdomain , provided reference assembly in appdomain, unloaded well.
if don't care creating memory leak, create unique names assembly (console1, console2 etc.)...
Comments
Post a Comment