Emit实现AOP示例

上传者: NqIceCoffee | 上传时间: 2023-02-21 00:41:39 | 文件大小: 33KB | 文件类型: ZIP
纯手工打造Emit实现AOP private static void OverrideMethods(TypeBuilder tb, MethodInfo method) { if (!method.IsPublic|| !method.IsVirtual || IsObjectMethod(method)) return; Type[] paramTypes = GetParameterTypes(method); MethodAttributes attr = MethodAttributes.Public | MethodAttributes.Family | MethodAttributes.HideBySig | MethodAttributes.Virtual; MethodBuilder mb = tb.DefineMethod(method.Name, attr, method.ReturnType, paramTypes); LocalBuilder result = null; ILGenerator il = mb.GetILGenerator(); bool is_void = method.ReturnType != typeof(void); if (is_void == false) result = il.DeclareLocal(method.ReturnType); object[] attrs = method.GetCustomAttributes(typeof(AspectAttribute), false); if (attrs != null) { //初始化所有当前方法用到的参数object[] CreateLocalParameterArr(il, paramTypes); //初始化AspectContext Type ctxType = typeof(AspectContext); ConstructorInfo info = ctxType.GetConstructor(Type.EmptyTypes); var ctx = il.DeclareLocal(ctxType); il.Emit(OpCodes.Newobj, info); il.Emit(OpCodes.Stloc, ctx); //给AspectContext的参数值属性ParameterArgs赋值 var propMethod = ctxType.GetMethod("set_ParameterArgs"); il.Emit(OpCodes.Ldloc, ctx); il.Emit(OpCodes.Ldloc_0); il.Emit(OpCodes.Call, propMethod); int m = attrs.Length; LocalBuilder[] lbs = new LocalBuilder[m]; MethodInfo[] endInvokeMethods = new MethodInfo[m]; //初始化标记的横切对象,并调用横切对象的BeforeInvoke方法 for (int i = 0; i < m; i++) { var tmpType = attrs[i].GetType(); var aspect = il.DeclareLocal(tmpType); ConstructorInfo tmpInfo = tmpType.GetConstructor(Type.EmptyTypes); il.Emit(OpCodes.Newobj, tmpInfo); il.Emit(OpCodes.Stloc, aspect); var before_invoke_method = tmpType.GetMethod("BeforeInvoke"); endInvokeMethods[i] = tmpType.GetMethod("AfterInvoke"); il.Emit(OpCodes.Ldloc, aspect); il.Emit(OpCodes.Ldloc, ctx); il.Emit(OpCodes.Callvirt, before_invoke_method); il.Emit(OpCodes.Nop); lbs[i] = aspect; } //类对象,参数值依次入栈 for (int i = 0; i <= paramTypes.Length; i++) il.Emit(OpCodes.Ldarg, i); //调用基类的方法 il.Emit(OpCodes.Call, method); //如果有返回值,保存返回值到局部变量 if (is_void == false) il.Emit(OpCodes.Stloc, result); //调用横切对象的AfterInvoke方法 for (int i = 0; i < m; i++) { il.Emit(OpCodes.Ldloc, lbs[i]); il.Emit(OpCodes.Ldloc, ctx); il.Emit(OpCodes.Callvirt, endInvokeMethods[i]); il.Emit(OpCodes.Nop); } //如果有返回值,则把返回值压栈 if (is_void == false) il.Emit(OpCodes.Ldloc, result); //返回 il.Emit(OpCodes.Ret); } }

文件下载

资源详情

[{"title":"( 13 个子文件 33KB ) Emit实现AOP示例","children":[{"title":"ConsoleApp","children":[{"title":"bin","children":[{"title":"Debug","children":[{"title":"ConsoleApp.pdb <span style='color:#111;'> 19.50KB </span>","children":null,"spread":false},{"title":"TestA_Aop_Assmely.dll <span style='color:#111;'> 1.50KB </span>","children":null,"spread":false},{"title":"ConsoleApp.vshost.exe <span style='color:#111;'> 11.32KB </span>","children":null,"spread":false},{"title":"ConsoleApp.exe <span style='color:#111;'> 9.50KB </span>","children":null,"spread":false},{"title":"ConsoleApp.vshost.exe.manifest <span style='color:#111;'> 490B </span>","children":null,"spread":false},{"title":"AspNet.App.Emit.dll <span style='color:#111;'> 2.00KB </span>","children":null,"spread":false}],"spread":true}],"spread":true},{"title":"obj","children":[{"title":"Debug","children":[{"title":"ConsoleApp.pdb <span style='color:#111;'> 19.50KB </span>","children":null,"spread":false},{"title":"ResolveAssemblyReference.cache <span style='color:#111;'> 2.46KB </span>","children":null,"spread":false},{"title":"ConsoleApp.csproj.FileListAbsolute.txt <span style='color:#111;'> 316B </span>","children":null,"spread":false},{"title":"ConsoleApp.exe <span style='color:#111;'> 9.50KB </span>","children":null,"spread":false},{"title":"Refactor","children":null,"spread":false},{"title":"TempPE","children":null,"spread":false}],"spread":true}],"spread":true},{"title":"Properties","children":[{"title":"AssemblyInfo.cs <span style='color:#111;'> 1.34KB </span>","children":null,"spread":false}],"spread":true},{"title":"Program.cs <span style='color:#111;'> 8.79KB </span>","children":null,"spread":false},{"title":"ConsoleApp.csproj <span style='color:#111;'> 2.30KB </span>","children":null,"spread":false}],"spread":true}],"spread":true}]

评论信息

免责申明

【只为小站】的资源来自网友分享,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,【只为小站】 无法对用户传输的作品、信息、内容的权属或合法性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论 【只为小站】 经营者是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。
本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二条之规定,若资源存在侵权或相关问题请联系本站客服人员,zhiweidada#qq.com,请把#换成@,本站将给予最大的支持与配合,做到及时反馈和处理。关于更多版权及免责申明参见 版权及免责申明