DO NOT DEAL WITH THIS MEMBER
Mar 11, 2017 04:26 PM
(This post was last modified: Mar 11, 2017 04:28 PM by jujuger.)
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:
I am trying to create the most realistic genetic-algorithm for evolution.
All of my what.gif
Try to explain what you're trying to do again
Yes, you can do this with codedom to compile the code in memory and reflection to run it.
Lotta blood to let, peace to make, give a fuk about a check
As BahNahNah said you can do it with CodeDom make sure set GenerateExecutable (CompilerParameters) property to false.
(Mar 11, 2017 06:33 PM).cYRil. 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.
DO NOT DEAL WITH THIS MEMBER
(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.
@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.
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?
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 =
new Microsoft.CSharp.CSharpCodeProvider())
{
var res = foo.CompileAssemblyFromSource(
new System.CodeDom.Compiler.CompilerParameters()
{
GenerateInMemory = true
},
"public class FooClass { public string Execute() { return \"output!\";}}"
);
var type = res.CompiledAssembly.GetType("FooClass");
var obj = Activator.CreateInstance(type);
var output = type.GetMethod("Execute").Invoke(obj, new object[] { });
}
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?
Compile it with CodeDom like tghe others explained but Invoke the EntryPoint of the program thats nicer xD
DO NOT DEAL WITH THIS MEMBER
Use Reflection.Emit to write IL to an assembly in memory and invoke said assembly.
Would be awesome if you share your results when done OP
Have a cool signature? hit me up and I might use it.
not sure if already replied but an example http://pastebin.com/5QzCKckB
"If i had the abilities to neg fuck you now i would - I really do not care what you think. Go away." ~Armada~
Mar 23, 2017 10:44 AM
(This post was last modified: Mar 23, 2017 10:45 AM by Washi.)
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/com...ith-roslyn
Good luck!
|