/// <summary> /// 修改dll里所有符合条件的方法 /// </summary> /// <typeparam name="T">注入的方法类</typeparam> /// <param name="dllFilePath">dll路径</param> /// <param name="aoptype">修改方法的类型</param> public static void Work <T>(string dllFilePath, AopType aoptype) where T : BaseAopAttribute, new() { using (AssemblyDefinition ass = AssemblyDefinition.ReadAssembly(dllFilePath)) { var typere = ass.MainModule.ImportReference(typeof(T)); foreach (var type in ass.MainModule.Types) { IEnumerable <MethodDefinition> meths = type.Methods.Where(a => (((AopType)(a.IsGetter?4:(a.IsSetter?8:(a.IsConstructor?2:1)))) | aoptype) == aoptype); List <MethodDefinition> methods = new List <MethodDefinition>(); foreach (var meth in type.Methods.Where(a => (((AopType)(a.IsGetter ? 4 : (a.IsSetter ? 8 : (a.IsConstructor ? 2 : 1)))) | aoptype) == aoptype)) { var newmeth = EditMethod(meth, new List <TypeReference> { typere }, ass.MainModule); if (newmeth != null) { methods.Add(newmeth); } } methods.ForEach(m => type.Methods.Add(m)); } ass.Write(dllFilePath + ".temp"); } File.Delete(dllFilePath); File.Copy(dllFilePath + ".temp", dllFilePath, true); File.Delete(dllFilePath + ".temp"); }
/// <summary> /// 注入aop方法 /// </summary> /// <param name="aopType"></param> /// <param name="type"></param> /// <returns></returns> public static InjectionMember[] GetInjectionMembers(AopType aopType, Type type) { var members = new List <InjectionMember>(); switch (aopType) { case AopType.VirtualMethodInterceptor: members.Add(new Interceptor <VirtualMethodInterceptor>()); break; case AopType.InterfaceInterceptor: members.Add(new Interceptor <InterfaceInterceptor>()); break; case AopType.TransparentProxyInterceptor: members.Add(new Interceptor <TransparentProxyInterceptor>()); break; case AopType.None: return(members.ToArray()); } members.AddRange(type.GetCustomAttributes() .Where(item => item.GetType().IsSubclassOf(typeof(UnityAopAttribute))) .Cast <UnityAopAttribute>() .Select(item => new InterceptionBehavior(item))); return(members.ToArray()); }
private static IEnumerable <CustomAttribute> FilterAttribute(IEnumerable <CustomAttribute> attributes, AopType type, bool isDefault) { return(attributes.Where(a => { var op = a.Properties.SingleOrDefault(b => b.Name == "Type"); if (null == op.Name) { return isDefault || false; } var opv = op.Argument.Value; int i = Convert.ToInt32(opv); AopType aop = ((AopType)i) & type; return aop == type; })); }
/// <summary> /// Aop Exception /// </summary> /// <param name="ex"></param> /// <param name="name"></param> public static void AopException(Exception ex, string name, AopType type, ConfigModel config, object model = null) { IFastAop aop = ServiceContext.Engine.Resolve <IFastAop>(); if (aop != null) { var context = new ExceptionContext(); context.name = name; context.type = type; context.ex = ex; context.dbType = config?.DbType; context.model = model; aop.Exception(context); } }
/// <summary> /// Aop Before /// </summary> /// <param name="sql"></param> /// <param name="name"></param> /// <param name="param"></param> /// <param name="config"></param> public static void AopBefore(List <string> tableName, string sql, List <DbParameter> param, ConfigModel config, bool isRead, AopType type, object model = null) { if (FastMap.fastAop != null) { var context = new BeforeContext(); if (tableName != null) { context.tableName = tableName; } context.sql = sql; if (param != null) { context.param = param; } context.dbType = config.DbType; context.isRead = isRead; context.isWrite = !isRead; context.model = model; FastMap.fastAop.Before(context); } }
/// <summary> /// Aop Map After /// </summary> /// <param name="sql"></param> /// <param name="name"></param> /// <param name="param"></param> /// <param name="config"></param> public static void AopMapAfter(string mapName, string sql, DbParameter[] param, ConfigModel config, AopType type, object data) { if (FastMap.fastAop != null) { var context = new MapAfterContext(); context.mapName = mapName; context.sql = sql; context.type = type; if (param != null) { context.param = param.ToList(); } context.dbType = config.DbType; context.result = data; FastMap.fastAop.MapAfter(context); } }
/// <summary> /// Aop Map Before /// </summary> /// <param name="sql"></param> /// <param name="name"></param> /// <param name="param"></param> /// <param name="config"></param> public static void AopMapBefore(string mapName, string sql, DbParameter[] param, ConfigModel config, AopType type) { if (FastMap.fastAop != null) { var context = new MapBeforeContext(); context.mapName = mapName; context.sql = sql; context.type = type; if (param != null) { context.param = param.ToList(); } context.dbType = config.DbType; FastMap.fastAop.MapBefore(context); } }
/// <summary> /// aop Exception /// </summary> /// <param name="ex"></param> /// <param name="name"></param> public static void AopException(Exception ex, string name, ConfigModel config, AopType type, object model = null) { if (FastMap.fastAop != null) { var context = new ExceptionContext(); context.dbType = context.dbType; context.ex = ex; context.name = name; context.type = type; context.model = model; FastMap.fastAop.Exception(context); } }
/// <summary> /// Aop After /// </summary> /// <param name="sql"></param> /// <param name="name"></param> /// <param name="param"></param> /// <param name="config"></param> public static void AopAfter(List <string> tableName, string sql, List <DbParameter> param, ConfigModel config, bool isRead, AopType type, object result, object model = null) { IFastAop aop = ServiceContext.Engine.Resolve <IFastAop>(); if (aop != null) { var context = new AfterContext(); if (tableName != null) { context.tableName = tableName; } context.sql = sql; if (param != null) { context.param = param; } context.dbType = config.DbType; context.isRead = isRead; context.isWrite = !isRead; context.result = result; context.type = type; context.model = model; aop.After(context); } }
/// <summary> /// Aop Map After /// </summary> /// <param name="sql"></param> /// <param name="name"></param> /// <param name="param"></param> /// <param name="config"></param> public static void AopMapAfter(string mapName, string sql, DbParameter[] param, ConfigModel config, AopType type, object data) { IFastAop aop = ServiceContext.Engine.Resolve <IFastAop>(); if (aop != null) { var context = new MapAfterContext(); context.mapName = mapName; context.sql = sql; context.type = type; if (param != null) { context.param = param.ToList(); } context.dbType = config.DbType; context.result = data; aop.MapAfter(context); } }