using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Reflection;
using System.Reflection.Emit;
using BLToolkit.Reflection;
using BLToolkit.Reflection.Emit;
namespace Adrian.Reflection
{
class Program
{
static void Main(string[] args)
{
string asName = "hw.exe";
AssemblyBuilderHelper assem = new AssemblyBuilderHelper(asName);
TypeBuilderHelper type = assem.DefineType("Emit.Program", TypeAttributes.Public, t.Object);
MethodBuilderHelper add = type.DefineMethod("Add", MethodAttributes.Public | MethodAttributes.Static, t.Int32, new Type[] { t.Int32Array });
add.MethodBuilder.DefineParameter(1, ParameterAttributes.None, "nums");
EmitHelper addIL = add.Emitter;
addIL.DeclareLocal(t.Int32);
addIL.DeclareLocal(t.Int32);
Label cmp = addIL.DefineLabel();
Label end = addIL.DefineLabel();
addIL
.ldc_i4_0
.stloc_0
.ldc_i4_0
.stloc_1
.MarkLabel(cmp)
.ldloc_1
.ldarg_0
.ldlen
.conv_i4
.clt
.brfalse_s(end)
.ldloc_0
.ldarg_0
.ldloc_1
.ldelem_i4
.add
.stloc_0
.ldloc_1
.ldc_i4_1
.add
.stloc_1
.br_s(cmp)
.MarkLabel(end)
.ldloc_0
.ret();
MethodBuilderHelper main = type.DefineMethod("Main", MethodAttributes.Public | MethodAttributes.Static, t.Void, new Type[] { t.StringArray });
main.MethodBuilder.DefineParameter(1, ParameterAttributes.None, "args");
EmitHelper mainIL = main.Emitter;
mainIL.DeclareLocal(t.Int32Array);
mainIL
.ldc_i4_3
.newarr(t.Int32)
.stloc_0
.ldloc_0
.ldc_i4_0
.ldc_i4_1
.stelem_i4
.ldloc_0
.ldc_i4_1
.ldc_i4_2
.stelem_i4
.ldloc_0
.ldc_i4_2
.ldc_i4_3
.stelem_i4
.ldloc_0
.call(add)
.call(typeof(Console), "WriteLine", t.Int32)
.ret();
type.Create();
assem.AssemblyBuilder.SetEntryPoint(main, PEFileKinds.ConsoleApplication);
assem.Save();
Assembly newAs = Assembly.LoadFile(Path.GetFullPath(asName));
newAs.EntryPoint.Invoke(null, new object[] { new string[] { } });
}
}
class t
{
private static Type _int32 = typeof(Int32);
private static Type _int32Array = typeof(Int32[]);
private static Type _object = typeof(Object);
private static Type _void = typeof(void);
private static Type _string = typeof(String);
private static Type _stringArray = typeof(String[]);
public static Type StringArray
{
get { return t._stringArray; }
}
public static Type String
{
get { return t._string; }
}
public static Type Void
{
get { return t._void; }
}
public static Type Object
{
get { return t._object; }
}
public static Type Int32Array
{
get { return t._int32Array; }
}
public static Type Int32
{
get { return _int32; }
}
}
}