using System;
using System.Data;
using System.Reflection;
using System.Reflection.Emit;
namespace ConsoleApplicationReflection
{
class ClassBuilder
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
try
{
Console.WriteLine("Enter values:");
string numbers = Console.ReadLine();
string[] values = numbers.Split(',');
//生成一个类型,其中包含一个方法(ReturnSum),返回两数相加的结果
Type ClsType = CreateType("OurAssembly", "OurModule", "MathOps", "ReturnSum");
//基于反射,生成类型(MathOps)实例
object ClsTypeInstance = Activator.CreateInstance(ClsType);
//基于反射,查找类型(MathOps)中的方法(ReturnSum)
MethodInfo mi = ClsType.GetMethod("ReturnSum");
//方法需要的参数数组
object[] objs = new object[] { Int32.Parse(values[0].ToString()), Int32.Parse(values[1].ToString()) };
//执行方法
object obj = mi.Invoke(ClsTypeInstance, objs);
//下面2行代码和上面4行执行的结果一样
//object ClsTypeInstance = Activator.CreateInstance(ClsType);
//object obj = MathOpsClass.InvokeMember("ReturnSum", BindingFlags.InvokeMethod, null, ClsTypeInstance, new object[] { Int32.Parse(values[0].ToString()), Int32.Parse(values[1].ToString()) });
Console.WriteLine("Sum: {0}", obj.ToString());
}
catch (Exception ex)
{
Console.WriteLine("An error occured: {0}", ex.Message);
}
Console.ReadLine();
}
/// <summary>
/// 动态生成程序集和类型
/// </summary>
/// <param name="assemblyName">程序集名称</param>
/// <param name="moduleName">模块名称</param>
/// <param name="className">类型名称</param>
/// <param name="methodName">方法名称</param>
/// <returns></returns>
public static Type CreateType(string assemblyName, string moduleName, string className, string methodName)
{
try
{
AssemblyName name = new AssemblyName();
name.Name = assemblyName;
AppDomain domain = System.Threading.Thread.GetDomain();
AssemblyBuilder assBuilder = domain.DefineDynamicAssembly(name, AssemblyBuilderAccess.RunAndSave);
ModuleBuilder mb = assBuilder.DefineDynamicModule(moduleName);
TypeBuilder theClass = mb.DefineType(className, TypeAttributes.Public | TypeAttributes.Class);
//方法返回值类型
Type rtnType = typeof(int);
//方法参数类型
Type[] param = new Type[2];
param[0] = typeof(System.Int32);
param[1] = typeof(System.Int32);
//定义方法签名
MethodBuilder method = theClass.DefineMethod(methodName, MethodAttributes.Public, rtnType, param);
//定义方法实体
ILGenerator gen = method.GetILGenerator();
gen.DeclareLocal(typeof(int)); //.locals init ([0] int32 c)
gen.Emit(OpCodes.Ldarg_1); //IL_0000: ldarg.1
gen.Emit(OpCodes.Ldarg_2); //IL_0001: ldarg.2
gen.Emit(OpCodes.Add); //IL_0002: add
gen.Emit(OpCodes.Stloc_0); //IL_0003: stloc.0
gen.Emit(OpCodes.Ldloc_0); //IL_0004: ldloc.0
gen.Emit(OpCodes.Ret); //IL_0005: ret
//返回生成的类型
return theClass.CreateType();
}
catch (Exception ex)
{
Console.WriteLine("An error occured: {0}", ex.Message);
return null;
}
}
}
}
using System.Data;
using System.Reflection;
using System.Reflection.Emit;
namespace ConsoleApplicationReflection
{
class ClassBuilder
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
try
{
Console.WriteLine("Enter values:");
string numbers = Console.ReadLine();
string[] values = numbers.Split(',');
//生成一个类型,其中包含一个方法(ReturnSum),返回两数相加的结果
Type ClsType = CreateType("OurAssembly", "OurModule", "MathOps", "ReturnSum");
//基于反射,生成类型(MathOps)实例
object ClsTypeInstance = Activator.CreateInstance(ClsType);
//基于反射,查找类型(MathOps)中的方法(ReturnSum)
MethodInfo mi = ClsType.GetMethod("ReturnSum");
//方法需要的参数数组
object[] objs = new object[] { Int32.Parse(values[0].ToString()), Int32.Parse(values[1].ToString()) };
//执行方法
object obj = mi.Invoke(ClsTypeInstance, objs);
//下面2行代码和上面4行执行的结果一样
//object ClsTypeInstance = Activator.CreateInstance(ClsType);
//object obj = MathOpsClass.InvokeMember("ReturnSum", BindingFlags.InvokeMethod, null, ClsTypeInstance, new object[] { Int32.Parse(values[0].ToString()), Int32.Parse(values[1].ToString()) });
Console.WriteLine("Sum: {0}", obj.ToString());
}
catch (Exception ex)
{
Console.WriteLine("An error occured: {0}", ex.Message);
}
Console.ReadLine();
}
/// <summary>
/// 动态生成程序集和类型
/// </summary>
/// <param name="assemblyName">程序集名称</param>
/// <param name="moduleName">模块名称</param>
/// <param name="className">类型名称</param>
/// <param name="methodName">方法名称</param>
/// <returns></returns>
public static Type CreateType(string assemblyName, string moduleName, string className, string methodName)
{
try
{
AssemblyName name = new AssemblyName();
name.Name = assemblyName;
AppDomain domain = System.Threading.Thread.GetDomain();
AssemblyBuilder assBuilder = domain.DefineDynamicAssembly(name, AssemblyBuilderAccess.RunAndSave);
ModuleBuilder mb = assBuilder.DefineDynamicModule(moduleName);
TypeBuilder theClass = mb.DefineType(className, TypeAttributes.Public | TypeAttributes.Class);
//方法返回值类型
Type rtnType = typeof(int);
//方法参数类型
Type[] param = new Type[2];
param[0] = typeof(System.Int32);
param[1] = typeof(System.Int32);
//定义方法签名
MethodBuilder method = theClass.DefineMethod(methodName, MethodAttributes.Public, rtnType, param);
//定义方法实体
ILGenerator gen = method.GetILGenerator();
gen.DeclareLocal(typeof(int)); //.locals init ([0] int32 c)
gen.Emit(OpCodes.Ldarg_1); //IL_0000: ldarg.1
gen.Emit(OpCodes.Ldarg_2); //IL_0001: ldarg.2
gen.Emit(OpCodes.Add); //IL_0002: add
gen.Emit(OpCodes.Stloc_0); //IL_0003: stloc.0
gen.Emit(OpCodes.Ldloc_0); //IL_0004: ldloc.0
gen.Emit(OpCodes.Ret); //IL_0005: ret
//返回生成的类型
return theClass.CreateType();
}
catch (Exception ex)
{
Console.WriteLine("An error occured: {0}", ex.Message);
return null;
}
}
}
}