![]() |
|
[C#] Create and Execute C# code at runtime, without creating external processes or files - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [C#] Create and Execute C# code at runtime, without creating external processes or files (/showthread.php?tid=5572711) |
Create and Execute C# code at runtime, without creating external processes or files - jujuger - Mar 11, 2017 Now this is extremely strange, but I want to create and execute code at runtime, but the thing is, I want to create and execute the code at runtime INSIDE SAID PROGRAM, without compiling again OR creating an external process or file. Have the program create itself at runtime Impossible? background: Spoiler (Click to View) * - gigajew - Mar 11, 2017 All of my what.gif Try to explain what you're trying to do again * - BahNahNah - Mar 11, 2017 Yes, you can do this with codedom to compile the code in memory and reflection to run it. * - .cYRil. - Mar 11, 2017 As BahNahNah said you can do it with CodeDom make sure set GenerateExecutable (CompilerParameters) property to false. * - Zettabit - Mar 11, 2017 (Mar 11, 2017 06:33 PM)HellGate Wrote: As BahNahNah said you can do it with CodeDom make sure set GenerateExecutable (CompilerParameters) property to false. Even setting GenerateExecutable to false and GenerateInMemory to true, it will still create a file on disk. @OP, if you absolutely don't want any files to be created on the disk, you're going to have to look for alternatives. If it's not an issue, then this would be your best solution. * - jujuger - Mar 11, 2017 (Mar 11, 2017 06:54 PM)Zettabit Wrote: Even setting GenerateExecutable to false and GenerateInMemory to true, it will still create a file on disk. The IO write is a problem because I need the program to iterate thousands of times per second. It needs to be speedy. Is it possible? * - ShadowCloudGH - Mar 12, 2017 This is interesting, you could potentially compile from a string as per the previous suggestion using codeDom, however, I'm pretty sure you'll end up with files on disk in terms of temp files, something like this from stackoverflow : Code using (Microsoft.CSharp.CSharpCodeProvider foo = Do you really need the part that get compiled dynamically to be C#? Or could you look at using something like a python script and invoke python to run the script so you don't have a compilation issue? * - w4zt3d - Mar 12, 2017 Compile it with CodeDom like tghe others explained but Invoke the EntryPoint of the program thats nicer xD * - ForceJew - Mar 12, 2017 https://hackforums.net/showthread.php?tid=5419995 Check it out op * - Marvincmarvin. - Mar 14, 2017 Use Reflection.Emit to write IL to an assembly in memory and invoke said assembly. * - severos - Mar 15, 2017 Would be awesome if you share your results when done OP * - NyQuil - Mar 21, 2017 not sure if already replied but an example http://pastebin.com/5QzCKckB * - Washi - Mar 23, 2017 The CompilerParameters.GenerateInMemory property does not exactly do what its name implies. Even though you do not have to specify a path to the output file, the file will be written to a temporary directory instead, loaded as a System.Reflection.Assembly and put in the CompilerResults.CompiledAssembly property, hence creating the illusion of being it generated in memory. While this is often enough for the normal use of CodeDom's compiler services, for your case it might slow down a lot since a disk performs its read and write operations a lot slower than memory does. Since you want to create a "realistic" genetic algorithm, what you might wanna look for is some kind of incremental compilation, where you keep changing your source code according to the feedback you get and recompile on the fly. Have a look at Roslyn, the open source compiler platform developed and released by Microsoft. Not only does it allow you to truly compile to memory, but also allows you to parse and change C# syntax to be compiled using syntax trees, which might be really useful in your case. Here is a tutorial that I found on the web that might help you out a bit: http://www.tugberkugurlu.com/archive/compiling-c-sharp-code-into-memory-and-executing-it-with-roslyn Good luck! |