代理介绍和动态生成程序技术

代理的介绍:http://www.microsoft.com/china/community/program/originalarticles/techdoc/proxymod.mspx


动态生成程序技术

DynamicProxy(无特殊说明的化,都指Castle's DynamicProxy for .net中的DynamicProxy技术)是建立在动态程序集生成技术的基础之上的,也就是说程序在运行时动态生成IL代码并被编译执行。

 动态程序集生成技术 也就是说序在运行时动态生成IL代码并被编译执行

ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/cpguide/html/cpconemittingdynamicassemblies.htm

动态程序集 System.Reflection.Emit 命名空间中的一组托管类型,它们允许编译器或工具在运行时发出元数据和 Microsoft 中间语言 (MSIL),或者也可以允许它们在磁盘上生成可移植可执行 (PE) 文件。脚本引擎和编译器是此命名空间的主要用户。在本节中,由 System.Reflection.Emit 命名空间提供的功能称为反射发出。

反射发出提供下列服务:

  • 在运行时定义程序集,然后运行这些程序集并/或将它们保存到磁盘。
  • 在运行时定义新程序集中的模块,然后运行这些模块并/或将它们保存到磁盘。
  • 在运行时定义类型,创建这些类型的实例,并调用这些类型的方法。
  • 为定义的模块定义可由调试器和代码分析器这样的工具使用的符号信息。

如果你想了解更多内容:见System.Reflection.Emit下面

 改例子来自msdn,有兴趣大家运行下

下面是一个运行的代码示例: 见文件夹TypeResolve

 

代理介绍和动态生成程序集技术using System;
代理介绍和动态生成程序集技术
using System.Text;
代理介绍和动态生成程序集技术
using System.Threading;
代理介绍和动态生成程序集技术
using System.Reflection;
代理介绍和动态生成程序集技术
using System.Reflection.Emit;
代理介绍和动态生成程序集技术
代理介绍和动态生成程序集技术
//  手动写的类和程序  与动态的程序集相同的功能
代理介绍和动态生成程序集技术
代理介绍和动态生成程序集技术
// --- 
代理介绍和动态生成程序集技术
// class Point {
代理介绍和动态生成程序集技术
//   
代理介绍和动态生成程序集技术
//   private int x;
代理介绍和动态生成程序集技术
//   private int y;
代理介绍和动态生成程序集技术
//
代理介绍和动态生成程序集技术
//   public Point(int ix, int iy) {
代理介绍和动态生成程序集技术
//
代理介绍和动态生成程序集技术
//       this.x = ix;
代理介绍和动态生成程序集技术
//        this.y = iy;
代理介绍和动态生成程序集技术
//
代理介绍和动态生成程序集技术
//   }
代理介绍和动态生成程序集技术
//
代理介绍和动态生成程序集技术
//   public int DotProduct (Point p) {
代理介绍和动态生成程序集技术
//   
代理介绍和动态生成程序集技术
//       return ((this.x * p.x) + (this.y * p.y));
代理介绍和动态生成程序集技术
//
代理介绍和动态生成程序集技术
//   }
代理介绍和动态生成程序集技术
//
代理介绍和动态生成程序集技术
//   public static void PointMain() {
代理介绍和动态生成程序集技术
//     
代理介绍和动态生成程序集技术
//     Console.Write("Enter the 'x' value for point 1: "); 
代理介绍和动态生成程序集技术
//     int x1 = Convert.ToInt32(Console.ReadLine());
代理介绍和动态生成程序集技术
//     
代理介绍和动态生成程序集技术
//     Console.Write("Enter the 'y' value for point 1: ");
代理介绍和动态生成程序集技术
//     int y1 = Convert.ToInt32(Console.ReadLine());
代理介绍和动态生成程序集技术
//
代理介绍和动态生成程序集技术
//     Console.Write("Enter the 'x' value for point 2: "); 
代理介绍和动态生成程序集技术
//     int x2 = Convert.ToInt32(Console.ReadLine());
代理介绍和动态生成程序集技术
//     
代理介绍和动态生成程序集技术
//     Console.Write("Enter the 'y' value for point 2: ");
代理介绍和动态生成程序集技术
//     int y2 = Convert.ToInt32(Console.ReadLine());
代理介绍和动态生成程序集技术
//
代理介绍和动态生成程序集技术
//     Point p1 = new Point(x1, y1);
代理介绍和动态生成程序集技术
//     Point p2 = new Point(x2, y2);
代理介绍和动态生成程序集技术
//
代理介绍和动态生成程序集技术
//     Console.WriteLine("({0}, {1}) . ({2}, {3}) = {4}.",
代理介绍和动态生成程序集技术
//               x1, y1, x2, y2, p1.DotProduct(p2));
代理介绍和动态生成程序集技术
//   
代理介绍和动态生成程序集技术
//   }
代理介绍和动态生成程序集技术
//
代理介绍和动态生成程序集技术
// }
代理介绍和动态生成程序集技术
// ---
代理介绍和动态生成程序集技术

代理介绍和动态生成程序集技术
class AssemblyBuilderDemo 


 

相关文章: