/// <summary> /// As aop interface proxy. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="instance">The instance.</param> /// <param name="injectionDelegates">The injection delegates.</param> /// <returns></returns> public static object AsAopInterfaceProxy <T>(this T instance, MethodInjectionDelegates injectionDelegates = null) where T : class { return(InternalAsAopInterfaceProxy(instance, false, injectionDelegates) as T); }
/// <summary> /// As the aop interface proxy. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="instance">The instance.</param> /// <param name="reuseInstance">if set to <c>true</c> [reuse instance].</param> /// <param name="injectionDelegates">The injection delegates.</param> /// <returns></returns> private static object InternalAsAopInterfaceProxy <T>(this T instance, bool reuseInstance, MethodInjectionDelegates injectionDelegates) where T : class { var type = typeof(T); object proxiedInstance = null; try { instance.CheckNullObject(nameof(instance)); if (!proxyOptionsCollection.ContainsKey(type)) { lock (locker) { if (!proxyOptionsCollection.ContainsKey(type)) { AopProxyOptions proxyOptions = new AopProxyOptions { Instance = instance, MethodInjectionDelegates = injectionDelegates }; PrepareProxy <T>(proxyOptions); proxyOptionsCollection.Add(type, proxyOptions); proxiedInstance = CreateInstance(proxyOptions, instance); proxyInstances[type] = proxiedInstance; return(type.IsInterface ? proxiedInstance as T : proxiedInstance); } } } return((reuseInstance ? proxyInstances[type] : CreateInstance(proxyOptionsCollection[type], instance)) as T); } catch (Exception ex) { throw ex.Handle(new { T = type.FullName }); } }
/// <summary> /// Creates the aop interface proxy. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="injectionDelegates">The injection delegates.</param> /// <returns></returns> public static object CreateAopInterfaceProxy <T>(MethodInjectionDelegates injectionDelegates = null) where T : class, new() { return(InternalAsAopInterfaceProxy(new T(), false, injectionDelegates)); // Cannot return AS T. Because in this case, T would be class not interface, As T would always be failed so that only null can be returned. }