/// <inheritdoc />
        public object Create(object target, Type typeOfProxy,
                                                    Type[] additionalInterfacesOfProxy)
        {
            Check.MustNotNull("target", "target");
            Check.MustNotNull("typeToProxy", "typeToProxy");

            var aspects = _aspectsFinder.FindAspects(target.GetType());
            if (!aspects.Any())
            {
                return target;
            }

            var options = new ProxyGenerationOptions
            {
                Selector = new PointcutAspectInterceptorSelector()
            };
            foreach (var instance in this.GetMixinInstances(target.GetType(), aspects))
            {
                options.AddMixinInstance(instance);
            }

            var interceptors = this.GetInterceptors(target.GetType(), aspects);
            if (typeOfProxy.IsInterface)
            {
                return _proxyGenerator.CreateInterfaceProxyWithTarget(typeOfProxy,
                                                       additionalInterfacesOfProxy,
                                                       target, options, interceptors);
            }
            else
            {
                return _proxyGenerator.CreateClassProxyWithTarget(typeOfProxy,
                                                       additionalInterfacesOfProxy,
                                                       target, options, interceptors);
            }
        }
        public void CanCreateMixinWithClassInterceptors()
        {
            var options = new ProxyGenerationOptions();
            options.AddMixinInstance(new Dictionary<int, int>());

            var builder = new ContainerBuilder();
            builder.RegisterType<C>().EnableClassInterceptors(options);
            builder.RegisterType<AddOneInterceptor>();
            builder.RegisterType<AddTenInterceptor>();
            var container = builder.Build();
            var i = 10;
            var cpt = container.Resolve<C>(TypedParameter.From(i));

            var dict = cpt as IDictionary<int, int>;

            Assert.NotNull(dict);

            dict.Add(1, 2);

            Assert.Equal(2, dict[1]);

            dict.Clear();

            Assert.Empty(dict);
        }
示例#3
0
        public override object GetFieldInterceptionProxy()
		{
			var proxyGenerationOptions = new ProxyGenerationOptions();
			var interceptor = new LazyFieldInterceptor();
			proxyGenerationOptions.AddMixinInstance(interceptor);
			return ProxyGenerator.CreateClassProxy(PersistentClass, proxyGenerationOptions, interceptor);
		}
示例#4
0
		/// <summary>
		/// Creates a context - which is used to guid custom proxy
		/// generation.
		/// </summary>
		/// <param name="mixins">Array of mixins to be registered</param>
		/// <returns>A GeneratorContext instance</returns>
		private static ProxyGenerationOptions CreateProxyGenerationOptions(object[] mixins)
		{
			ProxyGenerationOptions options = new ProxyGenerationOptions();
			foreach (object mixin in mixins)
			{
				options.AddMixinInstance(mixin);
			}
			return options;
		}
示例#5
0
文件: Runner.cs 项目: bling/AOPDemo
 public void Mixin()
 {
     var gen = new ProxyGenerator();
     var opts = new ProxyGenerationOptions();
     opts.AddMixinInstance(new HelloWorld());
     opts.AddMixinInstance(new HelloWorld2());
     var poco = (IUser)gen.CreateClassProxy(typeof(Poco), new[] { typeof(IUser) }, opts);
     poco.Hello();
     poco.Hello2();
     poco.Hi();
 }
示例#6
0
        private void Foo1() 
        {
            ProxyGenerator generator = new ProxyGenerator();
            var options = new ProxyGenerationOptions();
            options.AddMixinInstance(new ClassA());
            ClassB objB = generator.CreateClassProxy<ClassB>(options, new MyCastleInterceptor());
            objB.ActionB();
            InterfaceA objA = objB as InterfaceA;
            objA.ActionA();

        }
示例#7
0
        private static AsanaTask GetTask()
        {
            var options = new ProxyGenerationOptions();
            var changeable = new Changeable();
            options.AddMixinInstance(changeable);
            var task = Generator.Instance.CreateClassProxy<AsanaTask>(options, new ChangeInterceptor(changeable));

            changeable.TrackChanges = true;
            return task;

            //return new AsanaTask();
        }
        public void ServiceClientInterceptionIsPossibleWithMixins()
        {
            var options = new ProxyGenerationOptions();
            options.AddMixinInstance(new Dictionary<int, int>());

            // Build the service-side container
            var sb = new ContainerBuilder();
            sb.RegisterType<TestService>().As<ITestService>();

            // Build the client-side container with interception
            // around the client proxy. Issue 361 was that there
            // seemed to be trouble around getting this to work.
            var cb = new ContainerBuilder();
            cb.Register(c => CreateChannelFactory()).SingleInstance();
            cb
                .Register(c => c.Resolve<ChannelFactory<ITestService>>().CreateChannel())
                .InterceptTransparentProxy(options, typeof(IClientChannel))
                .UseWcfSafeRelease();

            using (var sc = sb.Build())
            {
                // Start the self-hosted test service
                var host = CreateTestServiceHost(sc);
                host.Open();
                try
                {
                    using (var cc = cb.Build())
                    {
                        // Make a call through the client to the service -
                        // it should be intercepted.
                        var client = cc.Resolve<ITestService>();
                        var dict = client as IDictionary<int, int>;

                        Assert.IsNotNull(dict);

                        dict.Add(1, 2);

                        Assert.AreEqual(2, dict[1]);

                        dict.Clear();

                        Assert.IsEmpty(dict);
                    }
                }
                finally
                {
                    host.Close();
                }
            }
        }
示例#9
0
        private void Foo1()
        {
            var container = new WindsorContainer();
            container.Register(
                Component.For<FooService, IFooService>()
                );

            ProxyGenerator generator = new ProxyGenerator();
            var options = new ProxyGenerationOptions();
            options.AddMixinInstance(new FooService());
            FooController objB = generator.CreateClassProxy<FooController>(options, new FooInterceptor(new FooService()));

            IFooService objA = objB as IFooService;
            objA.Do();
        }
        public AggregateRoot Create(Type aggregateType)
        {
            if (!typeof(AggregateRoot).IsAssignableFrom(aggregateType))
                throw new ArgumentException("aggregateType must inherit AggregateRoot");

            var snapshotType = _dynamicSnapshotAssembly.FindSnapshotType(aggregateType);
            var snapshotableImplementer = _snapshotableImplementerFactory.Create(snapshotType);
            var generator = new ProxyGenerator();

            var options = new ProxyGenerationOptions();
            options.AddMixinInstance(snapshotableImplementer);

            var proxy = (AggregateRoot)generator.CreateClassProxy(aggregateType, options);
            ((IHaveProxyReference)proxy).Proxy = proxy;

            return proxy;
        }
		public object Create(CreationContext context)
		{
			if (model.Service.IsInterface)
			{
				var interceptor = new LazyComponentInterceptor(innerActivator, context);
				var mixin = new LazyTargetHostMixin(interceptor);
				var proxyOptions = new ProxyGenerationOptions();
				proxyOptions.AddMixinInstance(mixin);

				var targetInterface = proxyGenerator.CreateInterfaceProxyWithoutTarget(model.Service, Type.EmptyTypes,
																																						   proxyOptions);
				var proxy = proxyGenerator.CreateInterfaceProxyWithTargetInterface(model.Service, Type.EmptyTypes, targetInterface,
																																				   ProxyGenerationOptions.Default, interceptor);
				return proxy;
			}
			// now what?

			throw new NotImplementedException("Service type must be an interface");
		}
示例#12
0
        protected override object RunCore(IPluginServiceProvider serviceProvider)
        {
            CostLineType currentCostType = (CostLineType)serviceProvider.GetService(typeof(CostLineType));

            CostManagerFactory costManagerFactory = CostManagerFactory.GetInstance(currentCostType);

            object currentCostManagerObject = EAppRuntime.Instance.CurrentApp.ObjectContainer.Resolve(costManagerFactory.CurrentCostManagerType);

            ProxyGenerator proxyGenerator = new ProxyGenerator();

            var options = new ProxyGenerationOptions();

            options.AddMixinInstance(currentCostManagerObject);

            ICostManager costManager =
                (ICostManager)proxyGenerator.CreateClassProxy<CostManagerFactory>(options);

            costManager.AddCostLine();

            return costManager.CurrentDisplayedCostLines;
        }
示例#13
0
		protected override void CustomizeOptions(ProxyGenerationOptions context, IKernel kernel, 
			ComponentModel model, object[] arguments)
		{
			AspectDefinition aspect = (AspectDefinition) model.ExtendedProperties["aop.aspect"];

			if (aspect == null) return;

			MixinDefinitionCollection mixins = aspect.Mixins;

			foreach(MixinDefinition definition in mixins)
			{
				Type mixinType = definition.TypeReference.ResolvedType;
				
				try
				{
					context.AddMixinInstance( Activator.CreateInstance( mixinType ) );
				}
				catch(Exception e)
				{
					throw new ApplicationException("Could not instantiate mixin " + mixinType.FullName, e);
				}
			}
		}
        public void CanCreateMixinWithAttributeInterceptors()
        {
            var options = new ProxyGenerationOptions();
            options.AddMixinInstance(new Dictionary<int, int>());

            var builder = new ContainerBuilder();
            builder.RegisterType<C>().As<IHasIAndJ>().EnableInterfaceInterceptors(options);
            builder.RegisterType<AddOneInterceptor>();
            builder.RegisterType<AddTenInterceptor>();
            var cpt = builder.Build().Resolve<IHasIAndJ>();

            var dict = cpt as IDictionary<int, int>;

            Assert.NotNull(dict);

            dict.Add(1, 2);

            Assert.Equal(2, dict[1]);

            dict.Clear();

            Assert.Empty(dict);
        }
        public void InterceptsWithMixinWhenUsingExtendedPropertyAndType()
        {
            var options = new ProxyGenerationOptions();
            options.AddMixinInstance(new Dictionary<int, int>());

            var builder = new ContainerBuilder();
            builder.RegisterType<CustomerService>()
                .As<ICustomerService>()
                .EnableInterfaceInterceptors(options);
            var container = builder.Build();

            var cs = container.Resolve<ICustomerService>();
            var dict = cs as IDictionary<int, int>;

            Assert.NotNull(dict);

            dict.Add(1, 2);

            Assert.Equal(2, dict[1]);

            dict.Clear();

            Assert.Empty(dict);
        }
		protected static ProxyGenerationOptions CreateProxyGenerationOptionsFrom(ProxyOptions proxyOptions, IKernel kernel, CreationContext context, ComponentModel model)
		{
			var proxyGenOptions = new ProxyGenerationOptions();
			if (proxyOptions.Hook != null)
			{
				var hook = proxyOptions.Hook.Resolve(kernel, context);
				if(hook != null && hook is IOnBehalfAware)
				{
					((IOnBehalfAware)hook).SetInterceptedComponentModel(model);
				}
				proxyGenOptions.Hook = hook;
			}

			if (proxyOptions.Selector != null)
			{
				var selector = proxyOptions.Selector.Resolve(kernel, context);
				if (selector != null && selector is IOnBehalfAware)
				{
					((IOnBehalfAware)selector).SetInterceptedComponentModel(model);
				}
				proxyGenOptions.Selector = selector;
			}
#if (!SILVERLIGHT)
			if (proxyOptions.UseMarshalByRefAsBaseClass)
			{
				proxyGenOptions.BaseTypeForInterfaceProxy = typeof(MarshalByRefObject);
			}
#endif
			foreach (var mixInReference in proxyOptions.MixIns)
			{
				var mixIn = mixInReference.Resolve(kernel, context);
				proxyGenOptions.AddMixinInstance(mixIn);
			}

			return proxyGenOptions;
		}
示例#17
0
		private ProxyGenerationOptions CreateProxyGenerationOptions(Type service, ProxyOptions proxyOptions, IKernel kernel, CreationContext context)
		{
			var userProvidedSelector = (proxyOptions.Selector != null) ? proxyOptions.Selector.Resolve(kernel, context) : null;

			var proxyGenOptions = new ProxyGenerationOptions(wcfProxyGenerationHook)
			{
				Selector = new WcfInterceptorSelector(service, userProvidedSelector)
			};

			if (proxyOptions.MixIns != null)
			{
				foreach (IReference<object> mixInReference in proxyOptions.MixIns)
				{
					var mixIn = mixInReference.Resolve(kernel, context);
					proxyGenOptions.AddMixinInstance(mixIn);
				}
			}		

			return proxyGenOptions;
		}
示例#18
0
		public void Exception_during_method_out_ref_arguments_set_class_proxy_mixin_uncaught()
		{
			var options = new ProxyGenerationOptions();
			options.AddMixinInstance(new ClassHasMethodThrowException());
			var proxy = generator.CreateClassProxy<object>(options, new StandardInterceptor()) as IClassHasMethodThrowException;

			var param1 = 1;
			var param2 = "1";
			var exMsg = "";
			var retVal = 1;

			try
			{
				retVal = proxy.MethodWithRefParam(ref param1, out param2);
			}
			catch (Exception ex)
			{
				exMsg = ex.Message;
			}

			Assert.AreEqual("intentional exception", exMsg);
			Assert.AreEqual(1, retVal);
			Assert.AreEqual(23, param1);
			Assert.AreEqual("23", param2);
		}
 private ProxyGenerationOptions GetOptionsToMixinCallRouter(ICallRouter callRouter)
 {
     var options = new ProxyGenerationOptions(_ignoreCallRouterCallsHook);
     options.AddMixinInstance(callRouter);
     return options;
 }
		protected static ProxyGenerationOptions CreateProxyGenerationOptionsFrom(ProxyOptions proxyOptions, IKernel kernel, CreationContext context)
		{
			var proxyGenOptions = new ProxyGenerationOptions();
			if (proxyOptions.Hook != null)
			{
				var hook = proxyOptions.Hook.Resolve(kernel, context);
				proxyGenOptions.Hook = hook;
			}

			if (proxyOptions.Selector != null)
			{
				var selector = proxyOptions.Selector.Resolve(kernel, context);
				proxyGenOptions.Selector = selector;
			}
#if (!SILVERLIGHT)
			if (proxyOptions.UseMarshalByRefAsBaseClass)
			{
				proxyGenOptions.BaseTypeForInterfaceProxy = typeof(MarshalByRefObject);
			}
#endif
			foreach (var mixInReference in proxyOptions.MixIns)
			{
				var mixIn = mixInReference.Resolve(kernel, context);
				proxyGenOptions.AddMixinInstance(mixIn);
			}

			return proxyGenOptions;
		}
示例#21
0
 public IColoredShape CreateColoredShapeMixin(IHasShape shape, IHasColor color)
 {
     var options = new ProxyGenerationOptions();
     options.AddMixinInstance(shape);
     options.AddMixinInstance(color);
     var generator = new ProxyGenerator();
     var proxy = generator.CreateClassProxy(typeof(object), new[] { typeof(IColoredShape) }, options) as IColoredShape;
     return proxy;
 }
示例#22
0
		protected static ProxyGenerationOptions CreateProxyGenerationOptionsFrom(ProxyOptions proxyOptions)
		{
			ProxyGenerationOptions proxyGenOptions = new ProxyGenerationOptions();

			if (proxyOptions.Hook != null)
			{
				proxyGenOptions.Hook = new ProxyGenerationHookAdapter(proxyOptions.Hook);
			}

			if (proxyOptions.UseMarshalByRefAsBaseClass)
			{
				proxyGenOptions.BaseTypeForInterfaceProxy = typeof(MarshalByRefObject);
			}

			foreach (object mixIn in proxyOptions.MixIns)
			{
				proxyGenOptions.AddMixinInstance(mixIn);
			}

			return proxyGenOptions;
		}