/// <summary> /// 取得InvocieApi代理 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="model"></param> /// <returns></returns> public static ApiBase <T> GetProxyInstace <T>(T model, IInterception interception = null) where T : class { IApiRunner realSubject = GetInstace(model); return(ProxyFactory.CreateProxyInstance <ApiBase <T> >(realSubject, interception)); }
/// <summary> /// 注入拦截器 /// </summary> /// <param name="interception"></param> /// <param name="enablePreInterception">是否启用方法执行后拦截</param> /// <param name="enableAfterInterception">是否启用方法执行前拦截</param> /// <param name="enableAroundInterception"></param> public void InjectInterception(IInterception interception, bool enablePreInterception, bool enableAfterInterception, bool enableAroundInterception) { _interception = interception; _enablePreInterception = enablePreInterception; _enableAfterInterception = enableAfterInterception; _enableArroundInterception = enableAroundInterception; }
/// <summary> /// 增加拦截 /// </summary> /// <param name="interceptor"></param> public void AddInterception(IInterception interceptor) { if (interceptor == null) { throw new ArgumentNullException("interceptor", "can not be null"); } interceptors.Add(interceptor); }
public T CreateProxyInstance <T>(IInterception interception) where T : class { var serverType = typeof(T); var target = Activator.CreateInstance(serverType) as MarshalByRefObject; var aopRealProxy = new AopProxy(serverType, target); aopRealProxy.InjectInterception(interception, EnableAfterInterception, EnablePreInterception, EnableAroundInterception); return((T)aopRealProxy.GetTransparentProxy()); }
/// <summary> /// 取得動態產生的代理物件 /// </summary> /// <typeparam name="T">被代理物件型別</typeparam> /// <param name="realSubject">被代理物件</param> /// <param name="interception">使用攔截器</param> /// <returns></returns> public static T CreateProxyInstance <T>(object realSubject, IInterception interception = null) { CheckType(realSubject); var aopRealProxy = new AOPRealProxy(realSubject); if (interception != null) { aopRealProxy.InterceptionDI(interception); } return((T)aopRealProxy.GetTransparentProxy()); }
/// <summary> /// 拦截器 /// </summary> /// <param name="interceptor">拦截器</param> /// <returns>服务绑定数据</returns> public IBindData AddInterceptor(IInterception interceptor) { Guard.NotNull(interceptor, "interceptor"); lock (syncRoot) { GuardIsDestroy(); if (interception == null) { interception = new List <IInterception>(); } interception.Add(interceptor); } return(this); }
/// <summary> /// 是否是生效的 /// </summary> /// <param name="interception"></param> /// <returns></returns> private bool IsEnable(IMethodInvoke methodInvoke, IInterception interception) { if (!interception.Enable) { return(false); } foreach (var ret in interception.GetRequiredAttr()) { if (!methodInvoke.MethodBase.IsDefined(ret, false)) { return(false); } } return(true); }
protected void Success(IJointPoint jp, IInterception context, object returnValue) { }
public void InterceptionDi(IInterception interception) { _interception = interception; }
/// <summary> /// 增加一个拦截器 /// </summary> /// <param name="interceptor"></param> public void Add(IInterception interceptor) { interceptionBehaviors.Add(interceptor); }
public WrappingInterception(IWrappingInterception wrapper, IInterception toWrap) { _wrapper = wrapper; _toWrap = toWrap; }
public AopRealProxy(Type targetType, MarshalByRefObject target, IInterception interception) : base(targetType) { _target = target; _interception = interception; }
public DynamicTest(IInterception interceptor = null) { _interceptor = interceptor; }
/// <summary> /// Constructs a proxy generator /// </summary> /// <param name="expression">the expression to proxy</param> /// <param name="obj">the object the method gets called</param> /// <param name="interceptor">the interceptor, can be null</param> public ProxyGenerator(Expression expression, object obj, IInterception interceptor) : base(expression, BindingRestrictions.Empty, obj) { _interceptor = interceptor; }
protected void Exit(IJointPoint jp, IInterception reset, object returnValue) { }
private static IInterception WrapWith(this IInterception target, IWrappingInterception wrapper) { return(new WrappingInterception(wrapper, target)); }
protected void Error(IJointPoint jp, IInterception reset, Exception exception) { }
public WrappingInterception(IWrappingInterception wrapper, IInterception toWrap) { _wrapper = wrapper; _toWrap = toWrap; }
protected void Before(IInterception interception) { }
/// <summary> /// Creates the proxied object for the given object and applies the interceptor to the methods /// accessible by the Type T. /// </summary> /// <typeparam name="T">the type of the given, proxied and intercepted object</typeparam> /// <param name="obj">the object to proxy</param> /// <param name="interceptor">the interceptor applied to the proxied object method invocations</param> /// <returns>the proxied object</returns> public static T Create <T>(T obj, IInterception <T> interceptor) { var wrappedType = obj.GetType(); var interceptorType = typeof(IInterception <T>); FieldBuilder wrappedFieldBuilder, interceptorFieldBuilder; #region Create dynamic assembly var assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName(AssemblyName), AssemblyBuilderAccess.RunAndSave); var moduleBuilder = assemblyBuilder.DefineDynamicModule(ModuleName, AssemblyFullName); #endregion #region Create type builder var typeBuilder = moduleBuilder.DefineType( wrappedType.FullName + "Proxy", wrappedType.Attributes, wrappedType, wrappedType.GetInterfaces()); #endregion #region Create wrapped and interceptor field builder wrappedFieldBuilder = typeBuilder.DefineField( WrappedFieldName, wrappedType, FieldAttributes.Private | FieldAttributes.InitOnly); interceptorFieldBuilder = typeBuilder.DefineField( InterceptorFieldName, interceptorType, FieldAttributes.Private | FieldAttributes.InitOnly); #endregion #region Create proxy constructor var constructorBuilder = typeBuilder.DefineConstructor( MethodAttributes.Public | MethodAttributes.HideBySig, CallingConventions.Standard, new[] { wrappedType, interceptorType }); var il = constructorBuilder.GetILGenerator(); // call this.base() il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Call, wrappedType.GetConstructor(new Type[0])); // store argument obj into wrapped il.Emit(OpCodes.Ldarg_0); // this il.Emit(OpCodes.Ldarg_1); // wrapped il.Emit(OpCodes.Stfld, wrappedFieldBuilder); il.Emit(OpCodes.Ldarg_0); // this il.Emit(OpCodes.Ldarg_2); // interceptor il.Emit(OpCodes.Stfld, interceptorFieldBuilder); il.Emit(OpCodes.Ret); #endregion #region Create proxied methods // only intercept interface methods visible to interceptor IList interceptableInterfaces; if (typeof(T).IsInterface) { interceptableInterfaces = new ArrayList() { typeof(T) }; } else { interceptableInterfaces = new ArrayList(wrappedType.GetInterfaces()); } foreach (var interfaceType in wrappedType.GetInterfaces()) { foreach (var method in interfaceType.GetMethods()) { var methodInfo = wrappedType.GetMethod(method.Name); var parameterTypes = methodInfo .GetParameters() .Select(x => x.ParameterType) .ToArray(); var methodBuilder = typeBuilder.DefineMethod( methodInfo.Name, methodInfo.Attributes, methodInfo.ReturnType, parameterTypes); // Ensure that proxied method is called, even when proxied object is cast to implementation class // Needs the implementation class to mark methods as virtual if (methodInfo.IsVirtual) { typeBuilder.DefineMethodOverride(methodBuilder, methodInfo); } il = methodBuilder.GetILGenerator(); // before interceptor call if interceptor is present if ((interceptor != null) && (interceptableInterfaces.Contains(interfaceType))) { il.Emit(OpCodes.Ldarg_0); // load this il.Emit(OpCodes.Ldfld, interceptorFieldBuilder); // load interceptor field il.Emit(OpCodes.Ldarg_0); // load this il.Emit(OpCodes.Ldfld, wrappedFieldBuilder); // load wrapper field il.Emit(OpCodes.Ldstr, methodInfo.Name); // load intercepted method name il.Emit(OpCodes.Callvirt, interceptorFieldBuilder.FieldType.GetMethod(nameof(IInterception <T> .Before))); } il.Emit(OpCodes.Ldarg_0); // load this il.Emit(OpCodes.Ldfld, wrappedFieldBuilder); // load wrapper field // load all parameters for (byte i = 1; i <= parameterTypes.Length; i++) { il.Emit(OpCodes.Ldarg_S, i); } il.Emit(OpCodes.Callvirt, wrappedFieldBuilder.FieldType.GetMethod(methodInfo.Name)); // Void return type cannot be saved as local variable in method body if (typeof(void) != methodInfo.ReturnType) { var localBuilder = il.DeclareLocal(methodInfo.ReturnType); il.Emit(OpCodes.Stloc, localBuilder); il.Emit(OpCodes.Ldloc, localBuilder); } // after interceptor call if interceptor is present if ((interceptor != null) && (interceptableInterfaces.Contains(interfaceType))) { il.Emit(OpCodes.Ldarg_0); // load this il.Emit(OpCodes.Ldfld, interceptorFieldBuilder); // load interceptor field il.Emit(OpCodes.Ldarg_0); // load this il.Emit(OpCodes.Ldfld, wrappedFieldBuilder); // load wrapper field il.Emit(OpCodes.Ldstr, methodInfo.Name); // load intercepted method name il.Emit(OpCodes.Callvirt, interceptorFieldBuilder.FieldType.GetMethod(nameof(IInterception <T> .After))); } il.Emit(OpCodes.Ret); } } #endregion var createdType = typeBuilder.CreateType(); assemblyBuilder.Save(AssemblyFullName); return((T)Activator.CreateInstance(createdType, obj, interceptor)); }
/// <summary> /// 動態代理類的建構子 /// </summary> /// <param name="instace">需要被代理的物件(需繼承於MarshalByRefObject)</param> public AOPRealProxy(object instace) : base(instace.GetType()) { _target = (MarshalByRefObject)instace; _interception = new LogInterception(); }