Esempio n. 1
0
        private static void ApplyInterception(Type[] interceptorTypes, IActivatingEventArgs <object> e, bool interceptAdditionalInterfaces)
        {
            Type type = e.Instance.GetType();

            if (e.Component.Services.OfType <IServiceWithType>().Any(swt => !swt.ServiceType.GetTypeInfo().IsVisible) || type.Namespace == "Castle.Proxies")
            {
                return;
            }

            Type[] proxiedInterfaces = type.GetInterfaces().Where(i => i.GetTypeInfo().IsVisible).ToArray();
            if (!proxiedInterfaces.Any())
            {
                return;
            }

            Type theInterface = proxiedInterfaces.First();

            Type[] interfaces = proxiedInterfaces.Skip(1).ToArray();

            IList <IInterceptor> interceptorInstances = new List <IInterceptor>();

            foreach (Type interceptorType in interceptorTypes)
            {
                interceptorInstances.Add((IInterceptor)e.Context.Resolve(interceptorType));
            }

            if (interceptorInstances.Count > 0)
            {
                IInterceptor[] interceptors = interceptorInstances.ToArray();

                object interceptedInstance = interceptAdditionalInterfaces
                    ? generator.CreateInterfaceProxyWithTargetInterface(theInterface, interfaces, e.Instance, interceptors)
                    : generator.CreateInterfaceProxyWithTargetInterface(theInterface, e.Instance, interceptors);

                e.ReplaceInstance(interceptedInstance);
            }
        }
Esempio n. 2
0
 private void Handler(IActivatingEventArgs <IRegistryAccess> obj)
 {
     obj.ReplaceInstance(new RegistryAccess(Application.CompanyName, Application.ProductName));
 }