private Type CreateAgentTypeByInterface(Type interfaceType, Type implementType) { string typeName = GetTypeName(implementType); TypeAttributes attr = TypeAttributes.Class | TypeAttributes.Public; TypeBuilder typeBuilder = m_ModuleBuilder.DefineType(typeName, attr, null, new Type[] { interfaceType }); //获取所有的父接口 IList <Type> interfaces = new List <Type>(); TypeHelper.GetAllFatherInterfaces(interfaceType, interfaces); //获取所有属性 PropertyInfo[] pis = TypeHelper.GetAllPropeties(interfaces); //生成代理+属性的私有成员 FieldBuilder agent = null; FieldBuilder[] members = null; Initialize.Members(typeBuilder, implementType, pis, ref agent, ref members); //识别AOP标记 Type exType = null; Type authType = null; Type[] basicTypes = null; //识别类上做的标记 var atts = implementType.GetCustomAttributes(true); FilterAspect(atts, ref basicTypes, ref authType, ref exType); //处理接口中的所有方法(对方法进行IL植入) MethodInfo[] methods = TypeHelper.GetAllMethods(interfaces); foreach (MethodInfo method in methods) { //忽略所有属性的get和set方法 if (method.IsSpecialName) { continue; } ImplementMethodByInterface(typeBuilder, agent, method, basicTypes, authType, exType); } //处理接口中的所有属性(自定义标签的植入) for (int i = 0; i < pis.Length; i++) { ImplementProperty(typeBuilder, pis[i], agent, members[i]); } Type dynamicType = typeBuilder.CreateType(); //如果想看动态生成的实际类型,可放开此代码,就可在运行dll的目录找到 //m_AssemblyBuilder.Save("333.dll"); //保存到本地 return(dynamicType); }
private Type CreateAgentTypeByClass(Type bindType) { string typeName = GetTypeName(bindType); TypeAttributes attr = TypeAttributes.Class | TypeAttributes.Public; TypeBuilder typeBuilder = m_ModuleBuilder.DefineType(typeName, attr, bindType, Type.EmptyTypes); //获取所有的父接口 //IList<Type> interfaces = new List<Type>(); //GetAllFatherInterfaces(bindType, interfaces); //获取所有属性 PropertyInfo[] pis = TypeHelper.GetAllPropeties(new Type[] { bindType }); //生成代理+属性的私有成员 FieldBuilder agent = null; FieldBuilder[] members = null; Initialize.Members(typeBuilder, bindType, pis, ref agent, ref members); //识别AOP标记 Type exType = null; Type authType = null; Type[] basicTypes = null; //识别类上做的标记 var atts = bindType.GetCustomAttributes(true); FilterAspect(atts, ref basicTypes, ref authType, ref exType); //处理接口中的所有方法(对方法进行IL植入) MethodInfo[] methods = bindType.GetMethods(BindingFlags.Public | BindingFlags.Instance); foreach (MethodInfo method in methods) { if (method.Name.StartsWith("get_") || method.Name.StartsWith("set_")) { continue; } ImplementMethodByClass(typeBuilder, agent, method, basicTypes, authType, exType); } //处理接口中的所有属性(自定义标签的植入) //for (int i = 0; i < pis.Length; i++) // ImplementProperty(typeBuilder, pis[i], agent, members[i]); Type dynamicType = typeBuilder.CreateType(); //如果想看动态生成的实际类型,可放开此代码,就可在运行dll的目录找到 //m_AssemblyBuilder.Save(m_DllName); //保存到本地 return(dynamicType); }
private void ImplementMethodByInterface(TypeBuilder typeBuilder, FieldBuilder agent, MethodInfo method, Type[] basicTypes, Type authType, Type exType) { var pis = method.GetParameters(); Type[] paramTypes = pis.Select(c => c.ParameterType).ToArray(); //识别方法上的标记(方法上的标记覆盖类上的标记) var atts = agent.FieldType.GetMethod(method.Name, paramTypes).GetCustomAttributes(true); //实现接口的方法标记 MethodAttributes attr = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.NewSlot | MethodAttributes.Virtual | MethodAttributes.Final; MethodBuilder methodBuilder = typeBuilder.DefineMethod(method.Name, attr, method.ReturnType, paramTypes); //构建各个参数的描述 DefineParameters(methodBuilder, pis); //开始植入方法体 ILGenerator il = methodBuilder.GetILGenerator(); //定义返回值并初始化(如果有) bool is_void = false; LocalBuilder result = InitializeResult(il, method.ReturnType, ref is_void); FilterAspect(atts, ref basicTypes, ref authType, ref exType); //初始化上下文对象 LocalBuilder context = null; LocalBuilder obj_arr = null; //如果存在AOP标记,则开始初始化上下文对象 if (basicTypes != null || authType != null || exType != null) { context = Initialize.Context(il, paramTypes, ref obj_arr, method, agent); } //开始植入基本(执行前)的AOP代码 var basics = Initialize.ExecutingBasics(il, basicTypes, context); //开始植入认证的AOP代码 Label?lbl = null; Initialize.Authentication(il, authType, context, ref lbl); //开始植入异常(try)AOP代码 EmitHelper.ImplantBeginException(il, exType); //利用成员代理和当前的调用参数,获取真正执行的函数结果 Initialize.CallResult(il, agent, method, pis, paramTypes, result, is_void); //开始植入异常(catch)AOP代码 EmitHelper.ImplantCatchException(il, exType, context); //如果有标签,则把标签定位到本位置(认证AOP使用) if (lbl.HasValue) { il.MarkLabel(lbl.Value); } //对ref参数的值进行重新赋值 ReSetValue(il, paramTypes, obj_arr); //将本次执行的结果附加到当前的上下文环境中 Initialize.AppendContextResult(il, context, result); //开始植入基本(执行后)的AOP代码 Initialize.ImplantExecutedBasics(il, basics, basicTypes, context); //如果有返回值,则结果压栈 if (!is_void) { il.Emit(OpCodes.Ldloc, result); } //返回结果 il.Emit(OpCodes.Ret); }