예제 #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));
        }
예제 #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());
        }
예제 #3
0
 public object CreateClassProxyWithTarget(object target, ProxyGeneratorOptions options, IInterceptor[] interceptors)
 {
     return(ProtectedCreateClassProxy(target?.GetType(), target, options, interceptors));
 }
예제 #4
0
 public TClass CreateClassProxyWithTarget <TClass>(TClass target, ProxyGeneratorOptions options, params IInterceptor[] interceptors) where TClass : class
 {
     return((TClass)ProtectedCreateClassProxy(typeof(TClass), target, options, interceptors));
 }
예제 #5
0
 public object CreateClassProxy(Type parentType, ProxyGeneratorOptions options, IInterceptor[] interceptors)
 {
     return(ProtectedCreateClassProxy(parentType, null, options, interceptors));
 }
예제 #6
0
 public TClass CreateClassProxy <TClass>(ProxyGeneratorOptions options, params IInterceptor[] interceptors) where TClass : class
 {
     return((TClass)ProtectedCreateClassProxy(typeof(TClass), null, options, interceptors));
 }
예제 #7
0
 public ProxyTypeDefinition GetTypeDefinition(Type type, object target, ProxyGeneratorOptions options)
 {
     return(moduleDefinition.GetTypeDefinition(type, target?.GetType(), options));
 }
예제 #8
0
 public object CreateInterfaceProxyWithoutTarget(Type interfaceType, ProxyGeneratorOptions options, IInterceptor[] interceptors)
 {
     return(ProtectedCreateInterfaceProxy(interfaceType, null, options, interceptors));
 }
예제 #9
0
 public TInterface CreateInterfaceProxyWithoutTarget <TInterface>(ProxyGeneratorOptions options, params IInterceptor[] interceptors) where TInterface : class
 {
     return((TInterface)ProtectedCreateInterfaceProxy(typeof(TInterface), null, options, interceptors));
 }