Emit语法简单实现(含实例)

了解完Emit的基本概念后就应该实际动手操作一下了,就像C#代码一样Emit代码也不是都需要理解的,复杂的逻辑当然需要理解,但是像string str=”abc”;这样的就不用深究了吧,知道这是干嘛的就行了。
没有了解过Emit和IL的朋友请先看看下面两篇文章:
Emit基本概念介绍:http://blog.csdn.net/xiaouncle/article/details/52621815
轻松读懂IL:http://blog.csdn.net/xiaouncle/article/details/52770462

【无返回值】无参方法SayHello()

Emit代码:

static void Main(string[] args)
{
    AssemblyName assemblyName = new AssemblyName("assemblyName");
    AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave);
    ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("PersonModule", "Person.dll");
    TypeBuilder typeBuilder = moduleBuilder.DefineType("Person", TypeAttributes.Public);
    //*【不同点1】
    MethodBuilder sayHelloMethod = typeBuilder.DefineMethod("SayHello", MethodAttributes.Public, null, null);
    ILGenerator ilOfSayHello = sayHelloMethod.GetILGenerator();
    ilOfSayHello.Emit(OpCodes.Ldstr, "蝈蝈");
    ilOfSayHello.Emit(OpCodes.Call, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }));
    ilOfSayHello.Emit(OpCodes.Call, typeof(Console).GetMethod("ReadLine"));
    //没有返回值所有加OpCodes.Pop
    ilOfSayHello.Emit(OpCodes.Pop);
    ilOfSayHello.Emit(OpCodes.Ret);
    Type personType = typeBuilder.CreateType();
    assemblyBuilder.Save("Person.dll");
    object obj = Activator.CreateInstance(personType);
    MethodInfo methodInfo = personType.GetMethod("SayHello");
    //【不同点2】
    methodInfo.Invoke(obj, null); 
}

.Net Reflector转换后代码:

public class Person
{
    // Methods
    public void SayHello()
    {
        Console.WriteLine("蝈蝈");
        Console.ReadLine();
    }
}

【无返回值】静态无参方法SayHello()

Emit代码:

static void Main(string[] args)
{            
    AssemblyName assemblyName = new AssemblyName("assemblyName");
    AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave);
    ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("PersonModule", "Person.dll");
    TypeBuilder typeBuilder = moduleBuilder.DefineType("Person", TypeAttributes.Public);
    //*【不同点1】
    MethodBuilder sayHelloMethod = typeBuilder.DefineMethod("SayHello", MethodAttributes.Public | MethodAttributes.Static, null, null);
    ILGenerator ilOfSayHello = sayHelloMethod.GetILGenerator();
    ilOfSayHello.Emit(OpCodes.Ldstr, "蝈蝈");
    ilOfSayHello.Emit(OpCodes.Call, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }));
    ilOfSayHello.Emit(OpCodes.Call, typeof(Console).GetMethod("ReadLine"));
    //没有返回值所有加OpCodes.Pop
    ilOfSayHello.Emit(OpCodes.Pop);
    ilOfSayHello.Emit(OpCodes.Ret);
    Type personType = typeBuilder.CreateType();
    assemblyBuilder.Save("Person.dll");
    object obj = Activator.CreateInstance(personType);
    MethodInfo methodInfo = personType.GetMethod("SayHello");
    //【不同点2】
    methodInfo.Invoke(obj, null);  
 }

.Net Reflector转换后代码:

public class Person
{
    // Methods
    public static void SayHello()
    {
        Console.WriteLine("蝈蝈");
        Console.ReadLine();
    }
}

【无返回值】含参方法SayHello(string name)

Emit代码:

static void Main(string[] args)
{
    AssemblyName assemblyName = new AssemblyName("assemblyName");
    AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave);
    ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("PersonModule", "Person.dll");
    TypeBuilder typeBuilder = moduleBuilder.DefineType("Person", TypeAttributes.Public);
    //*【不同点1】
    MethodBuilder sayHelloMethod = typeBuilder.DefineMethod("SayHello", MethodAttributes.Public, null, new Type[] { typeof(string) });
    ILGenerator ilOfSayHello = sayHelloMethod.GetILGenerator();
    ilOfSayHello.Emit(OpCodes.Ldstr, "你好:{0}");
    //【不同点2】
    ilOfSayHello.Emit(OpCodes.Ldarg_1);
    ilOfSayHello.Emit(OpCodes.Call, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string), typeof(string) }));
    ilOfSayHello.Emit(OpCodes.Call, typeof(Console).GetMethod("ReadLine"));
    //没有返回值所有加OpCodes.Pop
    ilOfSayHello.Emit(OpCodes.Pop);
    ilOfSayHello.Emit(OpCodes.Ret);
    Type personType = typeBuilder.CreateType();
    assemblyBuilder.Save("Person.dll");
    object obj = Activator.CreateInstance(personType);
    MethodInfo methodInfo = personType.GetMethod("SayHello");
    //【不同点3】
    methodInfo.Invoke(obj, new object[] { "蝈蝈" }); 
}

.Net Reflector转换后代码:

public class Person
{
    // Methods
    public void SayHello(string text1)
    {
        Console.WriteLine("你好:{0}", text1);
        Console.ReadLine();
    }
}

【无返回值】静态含参方法SayHello(string name)

Emit代码:

static void Main(string[] args)
{
    AssemblyName assemblyName = new AssemblyName("assemblyName");
    AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave);
    ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("PersonModule", "Person.dll");
    TypeBuilder typeBuilder = moduleBuilder.DefineType("Person", TypeAttributes.Public);
    //*【不同点1】
    MethodBuilder sayHelloMethod = typeBuilder.DefineMethod("SayHello", MethodAttributes.Public | MethodAttributes.Static, null, new Type[] { typeof(string) });
    ILGenerator ilOfSayHello = sayHelloMethod.GetILGenerator();
    ilOfSayHello.Emit(OpCodes.Ldstr, "你好:{0}");
    //【不同点2】
    ilOfSayHello.Emit(OpCodes.Ldarg_0);
    ilOfSayHello.Emit(OpCodes.Call, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string), typeof(string) }));
    ilOfSayHello.Emit(OpCodes.Call, typeof(Console).GetMethod("ReadLine"));
    //没有返回值所有加OpCodes.Pop
    ilOfSayHello.Emit(OpCodes.Pop);
    ilOfSayHello.Emit(OpCodes.Ret);
    Type personType = typeBuilder.CreateType();
    assemblyBuilder.Save("Person.dll");
    object obj = Activator.CreateInstance(personType);
    MethodInfo methodInfo = personType.GetMethod("SayHello");
    //【不同点3】
    methodInfo.Invoke(obj, new object[] { "蝈蝈" }); 
}

.Net Reflector转换后代码:

public class Person
{
    // Methods
    public static void SayHello(string text1)
    {
        Console.WriteLine("你好:{0}", text1);
        Console.ReadLine();
    }
}

【有返回值】无参方法SayHello()

Emit代码:

static void Main(string[] args)
{
    AssemblyName assemblyName = new AssemblyName("assemblyName");
    AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave);
    ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("PersonModule", "Person.dll");
    TypeBuilder typeBuilder = moduleBuilder.DefineType("Person", TypeAttributes.Public);
    //*【不同点1】
    MethodBuilder sayHelloMethod = typeBuilder.DefineMethod("SayHello", MethodAttributes.Public, typeof(string), null);
    ILGenerator ilOfSayHello = sayHelloMethod.GetILGenerator();
    ilOfSayHello.Emit(OpCodes.Ldstr, "蝈蝈");
    ilOfSayHello.Emit(OpCodes.Call, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }));
    ilOfSayHello.Emit(OpCodes.Call, typeof(Console).GetMethod("ReadLine"));
    //为什么加OpCodes.Pop
    ilOfSayHello.Emit(OpCodes.Pop);
    //返回值部分
    LocalBuilder local = ilOfSayHello.DeclareLocal(typeof(string));
    ilOfSayHello.Emit(OpCodes.Ldstr, "返回值");
    ilOfSayHello.Emit(OpCodes.Stloc_0, local);
    ilOfSayHello.Emit(OpCodes.Ldloc_0, local);
    ilOfSayHello.Emit(OpCodes.Ret);
    Type personType = typeBuilder.CreateType();
    assemblyBuilder.Save("Person.dll");
    object obj = Activator.CreateInstance(personType);
    MethodInfo methodInfo = personType.GetMethod("SayHello");
    //【不同点2】
    object result = methodInfo.Invoke(obj, null);
    Console.WriteLine("result=" + result.ToString());
    Console.ReadKey();
}

.Net Reflector转换后代码:

public class Person
{
    // Methods
    public string SayHello()
    {
        Console.WriteLine("蝈蝈");
        Console.ReadLine();
        return "返回值";
    }
}

【有返回值】静态无参方法SayHello()

Emit代码:

static void Main(string[] args)
{
    AssemblyName assemblyName = new AssemblyName("assemblyName");
    AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave);
    ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("PersonModule", "Person.dll");
    TypeBuilder typeBuilder = moduleBuilder.DefineType("Person", TypeAttributes.Public);
    //*【不同点1】
    MethodBuilder sayHelloMethod = typeBuilder.DefineMethod("SayHello", MethodAttributes.Public | MethodAttributes.Static, typeof(string), null);
    ILGenerator ilOfSayHello = sayHelloMethod.GetILGenerator();
    ilOfSayHello.Emit(OpCodes.Ldstr, "蝈蝈");
    ilOfSayHello.Emit(OpCodes.Call, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }));
    ilOfSayHello.Emit(OpCodes.Call, typeof(Console).GetMethod("ReadLine"));
    //为什么加OpCodes.Pop
    ilOfSayHello.Emit(OpCodes.Pop);
    //返回值部分
    LocalBuilder local = ilOfSayHello.DeclareLocal(typeof(string));
    ilOfSayHello.Emit(OpCodes.Ldstr, "返回值");
    ilOfSayHello.Emit(OpCodes.Stloc_0, local);
    ilOfSayHello.Emit(OpCodes.Ldloc_0, local);
    ilOfSayHello.Emit(OpCodes.Ret);
    Type personType = typeBuilder.CreateType();
    assemblyBuilder.Save("Person.dll");
    object obj = Activator.CreateInstance(personType);
    MethodInfo methodInfo = personType.GetMethod("SayHello");
    //【不同点2】
    object result = methodInfo.Invoke(obj, null);
    Console.WriteLine("result=" + result.ToString());
    Console.ReadKey();
}

.Net Reflector转换后代码:

public class Person
{
    // Methods
    public static string SayHello()
    {
        Console.WriteLine("蝈蝈");
        Console.ReadLine();
        return "返回值";
    }
}

【有返回值】含参方法SayHello(string name)

Emit代码:

static void Main(string[] args)
{
    AssemblyName assemblyName = new AssemblyName("assemblyName");
    AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave);
    ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("PersonModule", "Person.dll");
    TypeBuilder typeBuilder = moduleBuilder.DefineType("Person", TypeAttributes.Public);
    //*【不同点1】
    MethodBuilder sayHelloMethod = typeBuilder.DefineMethod("SayHello", MethodAttributes.Public, typeof(string), new Type[] { typeof(string) });
    ILGenerator ilOfSayHello = sayHelloMethod.GetILGenerator();
    ilOfSayHello.Emit(OpCodes.Ldstr, "蝈蝈");
    ilOfSayHello.Emit(OpCodes.Call, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }));
    ilOfSayHello.Emit(OpCodes.Call, typeof(Console).GetMethod("ReadLine"));
    //为什么加OpCodes.Pop
    ilOfSayHello.Emit(OpCodes.Pop);
    //返回值部分
    LocalBuilder local = ilOfSayHello.DeclareLocal(typeof(string));
    ilOfSayHello.Emit(OpCodes.Ldstr, "99999{0}");
    //【不同点2】
    ilOfSayHello.Emit(OpCodes.Ldarg_1);
    ilOfSayHello.Emit(OpCodes.Call, typeof(string).GetMethod("Format", new Type[] { typeof(string), typeof(string) }));
    ilOfSayHello.Emit(OpCodes.Stloc_0, local);
    ilOfSayHello.Emit(OpCodes.Ldloc_0, local);
    ilOfSayHello.Emit(OpCodes.Ret);
    Type personType = typeBuilder.CreateType();
    assemblyBuilder.Save("Person.dll");
    object obj = Activator.CreateInstance(personType);
    MethodInfo methodInfo = personType.GetMethod("SayHello");
    //【不同点3】
    object result = methodInfo.Invoke(obj, new object[] { "蝈蝈" });
    Console.WriteLine("result=" + result.ToString());
    Console.ReadKey();
}

.Net Reflector转换后代码:

public class Person
{
    // Methods
    public string SayHello(string text1)
    {
        Console.WriteLine("蝈蝈");
        Console.ReadLine();
        return string.Format("99999{0}", text1);
    }
}

【有返回值】静态含参方法SayHello(string name)

Emit代码:

static void Main(string[] args)
{
    AssemblyName assemblyName = new AssemblyName("assemblyName");
    AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave);
    ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("PersonModule", "Person.dll");
    TypeBuilder typeBuilder = moduleBuilder.DefineType("Person", TypeAttributes.Public);
    //*【不同点1】
    MethodBuilder sayHelloMethod = typeBuilder.DefineMethod("SayHello", MethodAttributes.Public | MethodAttributes.Static, typeof(string), new Type[] { typeof(string) });
    ILGenerator ilOfSayHello = sayHelloMethod.GetILGenerator();
    ilOfSayHello.Emit(OpCodes.Ldstr, "蝈蝈");
    ilOfSayHello.Emit(OpCodes.Call, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }));
    ilOfSayHello.Emit(OpCodes.Call, typeof(Console).GetMethod("ReadLine"));
    //为什么加OpCodes.Pop
    ilOfSayHello.Emit(OpCodes.Pop);
    //返回值部分
    LocalBuilder local = ilOfSayHello.DeclareLocal(typeof(string));
    ilOfSayHello.Emit(OpCodes.Ldstr, "99999{0}");
    //【不同点2】
    ilOfSayHello.Emit(OpCodes.Ldarg_0);
    ilOfSayHello.Emit(OpCodes.Call, typeof(string).GetMethod("Format", new Type[] { typeof(string), typeof(string) }));
    ilOfSayHello.Emit(OpCodes.Stloc_0, local);
    ilOfSayHello.Emit(OpCodes.Ldloc_0, local);
    ilOfSayHello.Emit(OpCodes.Ret);
    Type personType = typeBuilder.CreateType();
    assemblyBuilder.Save("Person.dll");
    object obj = Activator.CreateInstance(personType);
    MethodInfo methodInfo = personType.GetMethod("SayHello");
    //【不同点3】
    object result = methodInfo.Invoke(obj, new object[] { "蝈蝈" });
    Console.WriteLine("result=" + result.ToString());
    Console.ReadKey();
}

.Net Reflector转换后代码:

public class Person
{
    // Methods
    public static string SayHello(string text1)
    {
        Console.WriteLine("蝈蝈");
        Console.ReadLine();
        return string.Format("99999{0}", text1);
    }
}

实例代码免费下载地址:http://download.csdn.net/detail/xiaouncle/9649675
.Net Reflector Version 9.0.1.374 带注册机免费下载地址: http://download.csdn.net/detail/xiaouncle/9649783

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

changuncle

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值