轻量级AOP框架-移植python的装饰器(Decorator)到C#(编码篇)
2010-01-08 00:00:00 来源:WEB开发网然后需要完成的是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 }
- ››AOP的两个应用:实体集更新(DateEntityListUpdate...
- ››AOP的两个应用:实体集更新(DateEntityListUpdate...
- ››轻量级数据交换格式 JSON轻松入门
- ››轻量级系统 Lubuntu 10.04 发布
- ››AOP 的利器:ASM 3.0 介绍
- ››轻量级AOP框架-移植python的装饰器(Decorator)到C...
- ››轻量级AOP框架-移植python的装饰器(Decorator)到C...
- ››轻量级开发的成功秘诀,第 4 部分: 轻量级容器的比...
- ››轻量级开发的成功秘诀,第 5 部分: 在保守公司进行...
- ››轻量级开发的成功秘诀,第 6 部分: 持久性策略
- ››轻量级开发的成功秘诀,第 7 部分: Java 替代方案...
- ››轻量级开发的成功秘诀,第 8 部分: Seaside
更多精彩
赞助商链接