了解完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