Exemplo n.º 1
0
        protected object ProtectedCreateClassProxy(Type type, object target, ProxyGeneratorOptions options, IInterceptor[] interceptors)
        {
            if (type is null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            if (type.IsInterface)
            {
                throw new InvalidOperationException($"Unexpected interface for '{type.Name}'");
            }
            if (target != null && !type.IsAssignableFrom(target.GetType()))
            {
                throw new InvalidOperationException($"The target '{target.GetType().Name}' is not assignable from '{type.Name}'");
            }
            ProxyTypeDefinition typeDefinition = GetTypeDefinition(type, target, options);

            return(CreateProxy(typeDefinition, interceptors, target));
        }
Exemplo n.º 2
0
        protected object[] GetArguments(ProxyTypeDefinition typeDefinition, IInterceptor[] interceptors, object target, Type proxyType)
        {
            List <object> args = new List <object>();

            // interceptors
            args.Add(interceptors);

            // target
            if (target != null)
            {
                args.Add(target);
            }

            // interceptorSelector
            ProxyGeneratorOptions options             = typeDefinition.Options;
            IInterceptorSelector  interceptorSelector = options?.InterceptorSelector;

            if (interceptorSelector != null)
            {
                args.Add(interceptorSelector);
            }

            // mixins
            if (options != null && options.MixinInstances.Count > 0)
            {
                args.AddRange(options.MixinInstances);
            }

            // base ctor dependencies
            ConstructorInfo constructor = proxyType.GetConstructors()[0];
            var             parameters  = constructor.GetParameters();
            int             length      = parameters.Length;

            if (length > args.Count)
            {
                for (int i = args.Count; i < length; i++)
                {
                    args.Add(ConstructorInjectionResolver.Resolve(parameters[i]));
                }
            }

            return(args.ToArray());
        }
Exemplo n.º 3
0
 public object CreateClassProxyWithTarget(object target, ProxyGeneratorOptions options, IInterceptor[] interceptors)
 {
     return(ProtectedCreateClassProxy(target?.GetType(), target, options, interceptors));
 }
Exemplo n.º 4
0
 public TClass CreateClassProxyWithTarget <TClass>(TClass target, ProxyGeneratorOptions options, params IInterceptor[] interceptors) where TClass : class
 {
     return((TClass)ProtectedCreateClassProxy(typeof(TClass), target, options, interceptors));
 }
Exemplo n.º 5
0
 public object CreateClassProxy(Type parentType, ProxyGeneratorOptions options, IInterceptor[] interceptors)
 {
     return(ProtectedCreateClassProxy(parentType, null, options, interceptors));
 }
Exemplo n.º 6
0
 public TClass CreateClassProxy <TClass>(ProxyGeneratorOptions options, params IInterceptor[] interceptors) where TClass : class
 {
     return((TClass)ProtectedCreateClassProxy(typeof(TClass), null, options, interceptors));
 }
Exemplo n.º 7
0
 public ProxyTypeDefinition GetTypeDefinition(Type type, object target, ProxyGeneratorOptions options)
 {
     return(moduleDefinition.GetTypeDefinition(type, target?.GetType(), options));
 }
Exemplo n.º 8
0
 public object CreateInterfaceProxyWithoutTarget(Type interfaceType, ProxyGeneratorOptions options, IInterceptor[] interceptors)
 {
     return(ProtectedCreateInterfaceProxy(interfaceType, null, options, interceptors));
 }
Exemplo n.º 9
0
 public TInterface CreateInterfaceProxyWithoutTarget <TInterface>(ProxyGeneratorOptions options, params IInterceptor[] interceptors) where TInterface : class
 {
     return((TInterface)ProtectedCreateInterfaceProxy(typeof(TInterface), null, options, interceptors));
 }