Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A program that creates another program

Tags:

c#

.net

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

For example,

I write a program that accepts a string input from the user. Let's say user enter "Pluto". This program should then create a separate .exe that says "Hello Pluto" when executed.

How can I do this? If you could give example in C# and Windows Forms, it's better.

Thanks.

like image 803
zaidwaqi Avatar asked Jun 11 '10 20:06

zaidwaqi


2 Answers

Basically that is a compiler - just for a particularly simple language.

If you can express the final program in C#, the easiest solution is probably to use CSharpCodeProvider. That's what I do for Snippy, a little tool to help run code snippets easily for C# in Depth. The source code to Snippy is on the C# in Depth web site and can give you an idea of what you might do - basically you'd just want it to write the executable to a file instead of building it in memory.

Alternatively, look at the MSDN docs which have an example generating an executable.

like image 65
Jon Skeet Avatar answered Sep 30 '22 17:09

Jon Skeet


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 by using google - taking less than 20 sec)

like image 23
Femaref Avatar answered Sep 30 '22 18:09

Femaref