Ejemplo n.º 1
0
        public static object NewInstanceWithAdditionalInterfaces(
            Type type,
            ITypeInterceptor interceptor,
            IEnumerable <IInterceptionBehavior> interceptionBehaviors,
            IEnumerable <Type> additionalInterfaces,
            params object[] constructorParameters)
        {
            Guard.ArgumentNotNull(type, "type");
            Guard.ArgumentNotNull(interceptor, "interceptor");
            Guard.ArgumentNotNull(interceptionBehaviors, "interceptionBehaviors");
            Guard.ArgumentNotNull(additionalInterfaces, "additionalInterfaces");

            if (!interceptor.CanIntercept(type))
            {
                throw new ArgumentException(
                          string.Format(
                              CultureInfo.CurrentCulture,
                              Resources.InterceptionNotSupported,
                              type.FullName),
                          "type");
            }

            var behaviors = interceptionBehaviors.ToList();

            if (behaviors.Where(ib => ib == null).Count() > 0)
            {
                throw new ArgumentException(
                          string.Format(CultureInfo.CurrentCulture, Resources.NullBehavior),
                          "interceptionBehaviors");
            }

            Type implementationType = type;

            var activeBehaviors = behaviors.Where(ib => ib.WillExecute);

            Type[] allAdditionalInterfaces = GetAllAdditionalInterfaces(activeBehaviors, additionalInterfaces);

            Type interceptionType = interceptor.CreateProxyType(implementationType, allAdditionalInterfaces);

            var proxy = (IInterceptingProxy)Activator.CreateInstance(interceptionType, constructorParameters);

            foreach (IInterceptionBehavior interceptionBehavior in activeBehaviors)
            {
                proxy.AddInterceptionBehavior(interceptionBehavior);
            }

            return(proxy);
        }
        public override void PreBuildUp(IBuilderContext context)
        {
            Guard.ArgumentNotNull(context, "context");
            if (!this.CanIntercept(context))
            {
                return;
            }
            if (context.Existing != null)
            {
                return;
            }
            var interceptionPolicy       = FindInterceptionPolicy <ITypeInterceptionPolicy>(context);
            ITypeInterceptor interceptor = null;

            if (interceptionPolicy == null)
            {
                interceptor = context.NewBuildUp <IInterceptor>(typeof(IInterceptor).AssemblyQualifiedName) as ITypeInterceptor;
                if (null == interceptor)
                {
                    return;
                }
                if (!interceptor.CanIntercept(context.BuildKey.Type))
                {
                    return;
                }
                interceptionPolicy = new FixedTypeInterceptionPolicy(interceptor);
                context.Policies.Set <ITypeInterceptionPolicy>(interceptionPolicy, context.BuildKey);
                context.Policies.Clear <IInstanceInterceptionPolicy>(context.BuildKey);
            }
            var interceptionBehaviorsPolicy = FindInterceptionPolicy <IInterceptionBehaviorsPolicy>(context);

            if (null == interceptionBehaviorsPolicy)
            {
                var policyInjectionBehavior = new InterceptionBehavior <PolicyInjectionBehavior>();
                policyInjectionBehavior.AddPolicies(context.OriginalBuildKey.Type, context.BuildKey.Type, context.BuildKey.Name, context.Policies);
            }
        }