开发者

A program that creates another program

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.

Than开发者_StackOverflow社区ks.


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.


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)


Tentatively, you can also achive this throught he use of things in the System.Reflection.Emit namespace and I believe it's possible to provide the implementation of a method through the use of LINQ expression trees. Which is kind of 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 look a bit daunting at first, but it's really cool stuff, and you let the compiler generate the hard stuff, the method implementation. But you can't really create fields like this, that's gonna require some IL. Which is great fun but really tedious work.

DISCLAIMER:

I haven't tried to run the above code. But I know it goes something like that, it's been a while since I've done something like that.


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


Use Reflection.Emit.

http://msdn.microsoft.com/en-us/library/8ffc3x75.aspx

http://www.codeproject.com/KB/cs/DynamicCodeGeneration2.aspx

How to build a HelloWorld.exe using Reflection.Emit (it's less than half-page of code): Link

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜