开发学院软件开发Python 轻量级AOP框架-移植python的装饰器(Decorator)到C... 阅读

轻量级AOP框架-移植python的装饰器(Decorator)到C#(编码篇)

 2010-01-08 00:00:00 来源:WEB开发网   
核心提示: 然后需要完成的是DynamicTypeBuilder类,该类负责动态程序集,轻量级AOP框架-移植python的装饰器(Decorator)到C#(编码篇)(3),动态类型,动态方法的创建

然后需要完成的是DynamicTypeBuilder类,该类负责动态程序集,动态类型,动态方法的创建,具体实现如下:

01     internal class DynamicTypeBuilder {
02         //为动态程序集生成名称
03         private readonly string assemblyName = Guid.NewGuid().ToString();
04         //程序集主模块
05         private readonly string mainModuleName = "Main";
06  
07         public AssemblyBuilder AssemblyBuilder { get; private set; }
08  
09         public ModuleBuilder MainModuleBuilder { get; private set; }
10  
11         public DynamicTypeBuilder() {
12             AssemblyName an = new AssemblyName(assemblyName);
13             //构造一个可运行的动态程序集对象
14             AssemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(an, AssemblyBuilderAccess.Run);
15             //为该程序集构造主模块
16             MainModuleBuilder = AssemblyBuilder.DefineDynamicModule(mainModuleName);
17         }
18  
19         /// <summary>
20         /// 从Type类派生一个新的代理类
21         /// </summary>
22         /// <param name="type">基类</param>
23         /// <returns></returns>
24         public TypeBuilder CreateTypeBuilder(Type type) {
25             TypeBuilder typeBuilder = MainModuleBuilder.DefineType(type.Name + "_" + Guid.NewGuid().ToString(),
26                 TypeAttributes.Class | TypeAttributes.Public,
27                 type);
28             return typeBuilder;
29         }
30  
31         /// <summary>
32         /// 根据给定的方法对象和类型对象构造CodeGenerator
33         /// </summary>
34         /// <param name="typeBuilder"></param>
35         /// <param name="method"></param>
36         /// <returns></returns>
37         public CodeGenerator CreateMethodCodeGenerator(TypeBuilder typeBuilder, MethodInfo method) {
38             CodeGenerator cg = new CodeGenerator(typeBuilder,
39                 method.Name,
40                 MethodAttributes.Virtual | MethodAttributes.Public | MethodAttributes.ReuseSlot | MethodAttributes.HideBySig,
41                 CallingConventions.HasThis,
42                 method.ReturnType,
43                 method.GetParameters().Select(e => e.ParameterType).ToArray());
44             typeBuilder.DefineMethodOverride(cg.CurrentMethod, method);
45             return cg;
46         }
47 }

上一页  1 2 3 4 5 6  下一页

Tags:轻量级 AOP 框架

编辑录入:爽爽 [复制链接] [打 印]
[]
  • 好
  • 好的评价 如果觉得好,就请您
      0%(0)
  • 差
  • 差的评价 如果觉得差,就请您
      0%(0)
赞助商链接