예제 #1
0
        /// <summary>
        /// Creates a new interceptable dynamic proxy.
        /// </summary>
        /// <param name="type">The type to intercept.</param>
        /// <param name="interceptors">The <see cref="InterceptorRegistry" /> representing the type members decorated with interceptors.</param>
        /// <param name="serviceProvider">The <see cref="IServiceProvider" /> helping the creating object.</param>
        /// <returns>
        /// The interceptable dynamic proxy.
        /// </returns>
        public object Create(Type type, InterceptorRegistry interceptors, IServiceProvider serviceProvider)
        {
            Guard.ArgumentNotNull(type, nameof(type));
            Guard.ArgumentNotNull(interceptors, nameof(interceptors));
            Guard.ArgumentNotNull(serviceProvider, nameof(serviceProvider));

            if (CanIntercept(type) && !interceptors.IsEmpty)
            {
                var factory = _dynamicProxyFactoryCache.GetTypeFactory(type, interceptors);
                return(factory(interceptors, serviceProvider));
            }
            return(serviceProvider.GetService(type));
        }
예제 #2
0
        /// <summary>
        /// Creates a new interceptable dynamic proxy to wrap the specficied target instance.
        /// </summary>
        /// <param name="type">The interface type to intercept.</param>
        /// <param name="target">The target instance wrapped by the proxy.</param>
        /// <param name="interceptors">The <see cref="InterceptorRegistry" /> representing the type members decorated with interceptors.</param>
        /// <returns>
        /// The generated interceptable dynamic proxy.
        /// </returns>
        /// <exception cref="ArgumentNullException"> <paramref name="type"/> is null.</exception>
        /// <exception cref="ArgumentNullException"> <paramref name="target"/> is null.</exception>
        /// <exception cref="ArgumentNullException"> <paramref name="interceptors"/> is null.</exception>
        public object Wrap(Type type, object target, InterceptorRegistry interceptors)
        {
            Guard.ArgumentNotNull(type, nameof(type));
            Guard.ArgumentNotNull(target, nameof(target));
            Guard.ArgumentNotNull(interceptors, nameof(interceptors));

            if (CanIntercept(type))
            {
                var factory = _dynamicProxyFactoryCache.GetInstanceFactory(type, interceptors);
                return(factory(target, interceptors));
            }

            return(target);
        }
예제 #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DynamicProxyClassGenerator"/> class.
        /// </summary>
        /// <param name="typeToIntercept">The type to intercept.</param>
        /// <param name="interceptors">The <see cref="InterceptorRegistry"/> representing the type members decorated with interceptors.</param>
        /// <exception cref="ArgumentNullException">Specified <paramref name="typeToIntercept"/> is null.</exception>
        /// <exception cref="ArgumentNullException">Specified <paramref name="interceptors"/> is null.</exception>
        private DynamicProxyClassGenerator(Type typeToIntercept, InterceptorRegistry interceptors)
        {
            TypeToIntercept = Guard.ArgumentNotNull(typeToIntercept, nameof(typeToIntercept));
            Interceptors    = Guard.ArgumentNotNull(interceptors, nameof(interceptors));

            var assemblyName    = new AssemblyName($"AssemblyFor{typeToIntercept.Name}{GenerateSurfix()}");
            var assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);

            ModuleBuilder = assemblyBuilder.DefineDynamicModule($"{assemblyName}.dll");

            if (TypeToIntercept.IsInterface)
            {
                TypeBuilder = ModuleBuilder.DefineType($"{typeToIntercept.Name}{GenerateSurfix()}", TypeAttributes.Public, typeof(object), new Type[] { typeToIntercept, typeof(IInterceptableProxy) });
                TargetField = TypeBuilder.DefineField("_target", typeToIntercept, FieldAttributes.Private);
            }
            else
            {
                TypeBuilder = ModuleBuilder.DefineType($"{typeToIntercept.Name}{GenerateSurfix()}", TypeAttributes.Public, typeToIntercept, new Type[] { typeof(IInterceptorsInitializer), typeof(IInterceptableProxy) });
            }
            InterceptorsField = TypeBuilder.DefineField("_interceptors", typeof(InterceptorRegistry), FieldAttributes.Private);
        }
예제 #4
0
 /// <summary>
 /// Creates a <see cref="DynamicProxyClassGenerator"/> to generate dynamic proxy class based on interface based interception.
 /// </summary>
 /// <param name="type">The interface type to intercept.</param>
 /// <param name="interceptors">The <see cref="InterceptorRegistry"/> representing the type members decorated with interceptors.</param>
 /// <returns>The created <see cref="DynamicProxyClassGenerator"/>.</returns>
 /// <exception cref="ArgumentNullException">Specified <paramref name="type"/> is null.</exception>
 /// <exception cref="ArgumentException">Specified <paramref name="type"/> is  a sealed type.</exception>
 /// <exception cref="ArgumentNullException">Specified <paramref name="interceptors"/> is null.</exception>
 public static DynamicProxyClassGenerator CreateVirtualMethodGenerator(Type type, InterceptorRegistry interceptors)
 {
     Guard.ArgumentNotNull(type, nameof(type));
     if (type.IsSealed)
     {
         throw new ArgumentException(Resources.ExceptionSealedTypeNotAllowed, nameof(type));
     }
     return(new DynamicProxyClassGenerator(type, interceptors));
 }
예제 #5
0
 /// <summary>
 /// Creates a <see cref="DynamicProxyClassGenerator"/> to generate dynamic proxy class based on interface based interception.
 /// </summary>
 /// <param name="interface">The interface type to intercept.</param>
 /// <param name="interceptors">The <see cref="InterceptorRegistry"/> representing the type members decorated with interceptors.</param>
 /// <returns>The created <see cref="DynamicProxyClassGenerator"/>.</returns>
 /// <exception cref="ArgumentNullException">Specified <paramref name="interface"/> is null.</exception>
 /// <exception cref="ArgumentException">Specified <paramref name="interface"/> is not an interface type.</exception>
 /// <exception cref="ArgumentNullException">Specified <paramref name="interceptors"/> is null.</exception>
 public static DynamicProxyClassGenerator CreateInterfaceGenerator(Type @interface, InterceptorRegistry interceptors)
 {
     Guard.ArgumentNotNull(@interface, nameof(@interface));
     if ([email protected])
     {
         throw new ArgumentException(Resources.ExceptionArgumentNotInterface, nameof(@interface));
     }
     return(new DynamicProxyClassGenerator(@interface, interceptors));
 }
 public Func <InterceptorRegistry, IServiceProvider, object> GetTypeFactory(Type type, InterceptorRegistry interceptors)
 {
     if (_typeFactories.TryGetValue(type, out var factory))
     {
         return(factory);
     }
     lock (_typeFactories)
     {
         if (_typeFactories.TryGetValue(type, out factory))
         {
             return(factory);
         }
         var proxyType = DynamicProxyClassGenerator.CreateVirtualMethodGenerator(type, interceptors).GenerateProxyType();
         return(_typeFactories[type] = CreateTypeFactory(proxyType));
     }
 }
 public Func <object, InterceptorRegistry, object> GetInstanceFactory(Type type, InterceptorRegistry interceptors)
 {
     if (_instanceFactories.TryGetValue(type, out var factory))
     {
         return(factory);
     }
     lock (_instanceFactories)
     {
         if (_instanceFactories.TryGetValue(type, out factory))
         {
             return(factory);
         }
         var proxyType = DynamicProxyClassGenerator.CreateInterfaceGenerator(type, interceptors).GenerateProxyType();
         return(_instanceFactories[type] = CreateInstanceFactory(proxyType));
     }
 }