實現動態aop並註入 創建一個DynamicDispatchProxy類繼承DispatchProxy, public class DynamicDispatchProxy<T> : DispatchProxy { /// <summary> /// 目標類 /// </summary> priva ...
實現動態aop並註入
創建一個DynamicDispatchProxy類繼承DispatchProxy,
public class DynamicDispatchProxy<T> : DispatchProxy { /// <summary> /// 目標類 /// </summary> private T _this { get; set; } /// <summary> /// 進入前方法 /// </summary> private Action<string> _last { get; set; } /// <summary> /// 退出後方法 /// </summary> private Action<string, object?> _next { get; set; } private void last(string _v) { try { _last.Invoke(_v); } catch (System.Exception) { } } private void next(string _v, object _v1) { try { _next.Invoke(_v, _v1); } catch (System.Exception) { } } // private static ConcurrentDictionary<string, Delegate?> dic = new ConcurrentDictionary<string, Delegate?>(); protected override object? Invoke(MethodInfo? targetMethod, object?[]? args) { last(targetMethod.Name); object obj = null; try { /*var paramstype = targetMethod.GetParameters().Select(d => d.ParameterType).ToList(); var key =typeof(T).FullName+ targetMethod.Name + string.Join(' ', paramstype.Select(d => d.Name)); if (!dic.ContainsKey(key)) { List<ParameterExpression> listparame = new List<ParameterExpression>(); for (int i = 0; i < paramstype.Count; i++) { listparame.Add(Expression.Parameter(paramstype[i])); } MethodCallExpression method = Expression.Call(Expression.Constant(_this), targetMethod, listparame); Delegate? lambda = Expression.Lambda(method, listparame).Compile(); dic.TryAdd(key, lambda); } obj= dic[key].DynamicInvoke(args); next(targetMethod.Name,obj);
*/
obj= targetMethod.Invoke(_this,args);
next("",obj);
return obj; } catch (System.Exception ex) { throw ex; } } public T Create(){ ///BuildServiceProvider 獲取方法 var imp = IServiceCollectionHelp.GetService<T>(); ///攔截實現類的type var attributtype = imp.GetType().GetCustomAttribute<DynamicTypeAttribut>(); ///在這之前要先註入 攔截實現類才能獲取 Impintercept這是介面定義攔截前和攔截後 var Impintercept = IServiceCollectionHelp.GetService<IIntercept>(attributtype.type); object o = Create<T, DynamicDispatchProxy<T>>(); DynamicDispatchProxy<T> pro=(DynamicDispatchProxy<T>)o; ///賦值給上面委托 pro._this = imp; pro._last = Impintercept.Last; pro._next = Impintercept.Next; return (T)o; } }
創建一個特性類 來獲取攔截實現類的type !DynamicTypeAttribut
[AttributeUsage(AttributeTargets.Class)] public class DynamicTypeAttribut:Attribute { public Type type; public DynamicTypeAttribut(Type type){ this.type=type; } }
定義攔截介面 IIntercept
public interface IIntercept { void Last(string name); void Next(string name,object? obj); }
攔截的目標
public interface ITest { void Write(); int Add(int v1,int v2); }
///這裡是Intercept是實現類 [DynamicTypeAttribut(typeof(Intercept))] public class Test : ITest { public int Add(int v1, int v2) { Console.WriteLine(nameof(Add)); return v1+v2; } public void Write() { Console.WriteLine(this.GetHashCode()); } }
攔截方法的實現
public class Intercept : IIntercept { public void Last(string name) { Console.WriteLine(this.GetHashCode()); Console.WriteLine("進入"+name); } public void Next(string name, object? obj) { Console.WriteLine("推出"+name); if( obj !=null) Console.WriteLine("返回值"+obj); } }
註入
///註入攔截實現類 不然獲取不到 builder.Services.AddScoped< Intercept>(); ///這裡也要註入一次 不然也獲取不到 builder.Services.AddScoped<ITest, Test>(); var _ = new DynamicDispatchProxy<ITest>(); IServiceCollectionHelp.serviceProvider = builder.Services.BuildServiceProvider(); builder.Services.AddScoped(typeof(ITest), d => { return _.Create();///創建 })