Below is the sample code to do it:
using System;
using System.Reflection;
using System.Reflection.Emit;
public class TestClass
{
public static void Main()
{
AssemblyName asmname = new AssemblyName();
asmname.Name = "tempfile_Asm";
AssemblyBuilder asmbuild = System.Threading.Thread.GetDomain().
DefineDynamicAssembly(asmname, AssemblyBuilderAccess.RunAndSave); //
run and save only assembly
ModuleBuilder mb = asmbuild.DefineDynamicModule( "TestModule",
asmname.Name + ".dll" );
TypeBuilder tb = mb.DefineType( "MyType", TypeAttributes.Public );
ConstructorBuilder corb = tb.DefineDefaultConstructor(
MethodAttributes.Public );
MethodBuilder methb = tb.DefineMethod( "Test", MethodAttributes.Public,
null, null );
ILGenerator lgen = methb.GetILGenerator();
lgen.DeclareLocal( typeof( Object ) );
lgen.Emit( OpCodes.Newobj, corb );
lgen.Emit( OpCodes.Stloc_0 );
lgen.Emit( OpCodes.Ret );
tb.CreateType();
Console.WriteLine( "Saving assembly file " + asmname.Name + ".dll" );
asmbuild.Save( asmname.Name + ".dll" );
Assembly asm = Assembly.LoadFrom( asmname.Name + ".dll" );
Type t = asm.GetType( "MyType" );
Console.WriteLine( "t=" + t );
}
}
Thanks
Alicia[MSFT]
--------------------
>From: "Zanna" <znt.fabio@virgilio.it>
>Newsgroups: microsoft.public.dotnet.framework.clr
[quoted text clipped - 9 lines]
>X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
>Path:
cpmsftngxa07.phx.gbl!cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.su
l.t-online.de!newsfeed01.sul.t-online.de!t-online.de!fu-berlin.de!uni-berlin
.de!host188-20.pool21345.interbusiness.IT!not-for-mail
>Xref: cpmsftngxa07.phx.gbl microsoft.public.dotnet.framework.clr:8790
>X-Tomcat-NG: microsoft.public.dotnet.framework.clr
[quoted text clipped - 67 lines]
>--
>Math Parser : http://www.neodatatype.net