/// <summary> /// 执行方法 /// </summary> /// <param name="invocation"></param> /// <returns></returns> internal void Invoke(IInvocation invocation) { var st = new Stopwatch(); st.Start(); if (_pluginInstance != null && _classMember != null && _classMember.PluginMethod != null && _classMember.PluginMethod.TryGetValue(invocation.MethodInvocationTarget.Name, out MethodInfo methodInfo)) { LogEventProxy.FireLogRecord($@"执行二开方法:{methodInfo.DeclaringType?.Name}{methodInfo.Name}"); //插件 执行 invocation.ReturnValue = methodInfo.Invoke(_pluginInstance, invocation.Arguments); st.Stop(); LogEventProxy.FireLogRecord($@"执行二开方法:{methodInfo.DeclaringType?.Name}{methodInfo.Name} 耗时:{st.Elapsed.Milliseconds}ms"); } else { //产品 执行 invocation.Proceed(); st.Stop(); LogEventProxy.FireLogRecord($@"执行功能:{invocation.TargetType.Name}{invocation.MethodInvocationTarget.Name} 耗时:{st.Elapsed.Milliseconds}ms"); } }
/// <summary> /// /// </summary> /// <param name="invocation"></param> public void Intercept(IInvocation invocation) { var pluginInovker = GetInvoker(invocation.TargetType); try { //执行原对象中的方法 pluginInovker.Invoke(invocation); } catch (Exception e) { LogEventProxy.FireLogRecord(e.Message); throw; } }
/// <summary> /// 初始化工厂 /// </summary> static void InitFactory() { lock (PluginMenmberTable.SyncRoot) { PluginMenmberTable.Clear(); var st = new Stopwatch(); st.Start(); foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies().Where(q => q.GetCustomAttribute <PluginAssemblyAttribute>() != null)) { foreach (var plugType in assembly.GetExportedTypes().Where(q => q.IsClass && !q.IsAbstract && q.GetCustomAttribute <PluginClassAttribute>() != null)) { var plugAttr = plugType.GetCustomAttribute <PluginClassAttribute>(); var member = new PluginClassMember() { FromType = plugAttr.FromType, TargetType = plugType, PluginMethod = new Dictionary <string, MethodInfo>() }; foreach (var method in plugType.GetMethods().Where(q => q.IsPublic).Select(q => new { Method = q, PluginAttr = q.GetCustomAttribute <PluginMethodAttribute>() }).Where(q => q.PluginAttr != null)) { //收集扩展方法 member.PluginMethod[method.PluginAttr.MethodName] = method.Method; } PluginMenmberTable.Add(member.FromType, member); } } st.Stop(); IsCustomize = PluginMenmberTable.Count > 0; LogEventProxy.FireLogRecord($"加载AOP配置耗时:{st.Elapsed.Milliseconds}"); } }