Program that creates another program

I need to create a program that creates another program, but not a compiler.

For example,

I am writing a program that accepts string input from a user. Let them say that the user enters "Pluto." Then this program should create a separate .exe that says "Hello Pluto" at runtime.

How can i do this? If you could give an example in C # and Windows Forms, this is better.

Thanks.

+6
c #
source share
5 answers

This is basically a compiler - only for a particularly simple language.

If you can express the final program in C #, the easiest solution would probably be to use CSharpCodeProvider. This is what I am doing for Snippy, a small tool to help you easily run code snippets for C # in Depth. The Snippy source code on the C # website is in depth and can give you an idea of ​​what you can do - basically you just want it to write the executable to a file rather than create it in memory.

Alternatively, consider MSDN documents that have an example of creating an executable file.

+14
source share

The classes you are looking for are in the Microsoft.CSharp namespace

 CSharpCodeProvider codeProvider = new CSharpCodeProvider(); ICodeCompiler icc = codeProvider.CreateCompiler(); System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters(); parameters.GenerateExecutable = true; parameters.OutputAssembly = Output; CompilerResults results = icc.CompileAssemblyFromSource(parameters,SourceString); 

(from microsoft support found with google - less than 20 seconds)

+11
source share

In pre-order, you can also achieve this by using it in the System.Reflection.Emit namespace, and I find it possible to provide a method implementation using LINQ expression trees. Which type is neat:

 var assembly = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName("TestAssembly"), AssemblyBuilderAccess.RunAndSave); var mod = assembly.DefineDynamicModule("TestModule"); var type = mod.DefineType("TestType"); var method = type.DefineMethod("Increment", MethodAttributes.Public, typeof(int), Type.EmptyTypes); Expression<Func<int, int>> inc = (a) => a + 1; // this is cool inc.CompileToMethod(method); 

It might seem a bit complicated at first, but it is really cool stuff, and you let the compiler generate hard stuff - an implementation of the method. But you really cannot create fields that require some IL. This is a lot of fun, but very tiring work.

RENOUNCEMENT:

I did not try to run the above code. But I know that this is something like that, some time has passed since I did something like that.

+3
source share
0
source share

The easiest way would be to create (in advance) a very short simple program that reads a resource from itself and prints it. Then your application only needs to place this binary program on disk somewhere and call the UpdateResource function to change the line .. NET also has resource support, but I do not know any function that directly corresponds to UpdateResource.

0
source share

All Articles