¡¾·±ÌåÖÐÎÄ¡¿
¡¾ÉèΪÊ×Ò³¡¿
¡¾¼ÓÈëÊղء¿
µ±Ç°Î»Öãº.Net¼¼ÊõÊ×Ò³ >> Asp.Net¿ª·¢ >> Create Exe On Fly

Create Exe On Fly

2007-03-15 08:00:00  ×÷Õߣº  À´Ô´£º»¥ÁªÍø  ä¯ÀÀ´ÎÊý£º0  ÎÄ×Ö´óС£º¡¾´ó¡¿¡¾ÖС¿¡¾Ð¡¡¿
¼ò½é£ºCreating Exe on the Fly Submitted ByUser LevelDate of SubmissionKunal ChedaBeginner06/29/2001 This is probably the most wonderful this I have explored in .net. What happens here is a user can actu...
¹Ø¼ü×Ö£ºCreate Fly Exe On

Creating Exe on the Fly

Submitted ByUser LevelDate of SubmissionKunal ChedaBeginner06/29/2001

This is probably the most wonderful this I have explored in .net. What happens here is a user can actually make the EXE file on the fly. System.Runtime.Emit Namespace provides necessay class to do this.

After compiling this file and running it on the Console a new file is generated in your folder called TestAsm.exe. This Exe file print's a message "Hello World" on the Console.

Source Code:

RuntimeEmit.cs

using System;

using System.Runtime;

using System.Runtime.Emit;

Class RuntimeEmit

{

public static void Main(String [] args)

{

AppDomain ad = AppDomain.CurrentDomain;

AssemblyName am = new AssemblyName();

am.Name = "TestAsm";

AssemblyBuilder ab = ad.DefineDynamicAssembly(am,AssemblyBuilderAccess.Save);

ModuleBuilder mb = ab.DefineDynamicModule("testmod","TestAsm.exe");

TypeBuilder tb = mb.DefineType("mytype",TypeAttributes.Public);

MethodBuilder metb = tb.DefineMethod("hi",MethodAttributes.Public | MethodAttributes.Static,null,null);

mb.SetEntryPoint(metb);

ILGenerator il = metb.GetILGenerator();

il.EmitWriteLine("Hello World");

il.Emit(OpCodes.Ret);

tb.CreateType();

ab.Save("TestAsm.exe");

}

}

Save this file as RuntimeEmit.cs and Compile C:/>csc RuntimeEmit.cs and run this file C:\>RuntimeEmit

to run the Exe file generated C:\>TestAsm

ÔðÈα༭£ºadmin
±¾ÎÄÒýÓõØÖ·£º http://www.3pcode.com/net/2007/03/84239.htm
Ïà¹ØÎÄÕÂ