public DynamicProxyValueInterceptorContext()
 {
     var innerCatalog = new TypeCatalog(typeof(Customer));
     var interceptor = new FreezableInterceptor();
     interceptor.Freeze();
     var valueInterceptor = new DynamicProxyInterceptor(interceptor);
     Catalog = new InterceptingCatalog(innerCatalog, valueInterceptor);
     Container = new CompositionContainer(Catalog);
     Context();
 }
		public object Create(Type type, MocksRepository repository, IMockMixin mockMixinImpl, MockCreationSettings settings, bool createTransparentProxy)
		{
			var baseType = type.IsGenericType ? type.GetGenericTypeDefinition() : type;
			RuntimeTypeHandle proxyTypeHandle;
			var key = new ProxySourceRegistry.ProxyKey(
				baseType.TypeHandle, GetAdditionalInterfaceHandles(type, settings.AdditionalMockedInterfaces));
			if (!ProxySourceRegistry.ProxyTypes.TryGetValue(key, out proxyTypeHandle))
			{
				ThrowNoProxyException(baseType, settings.AdditionalMockedInterfaces);
			}

			var interceptor = new DynamicProxyInterceptor(repository);

			var proxyType = Type.GetTypeFromHandle(proxyTypeHandle);
			if (proxyType.IsGenericTypeDefinition)
				proxyType = proxyType.MakeGenericType(type.GetGenericArguments());

			var mockConstructorCall = settings.MockConstructorCall
				&& proxyType.BaseType != typeof(object)
				&& UninitializedObjectFactory.IsSupported;

			ConstructorInfo proxyCtor = null;
			if (!mockConstructorCall && settings.Args == null)
			{
				proxyCtor = proxyType.GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
					.First(ctor => ctor.IsPublic || ctor.IsFamily || ctor.IsFamilyOrAssembly);
				settings.Args = proxyCtor.GetParameters()
					.TakeWhile(p => p.ParameterType != typeof(IInterceptor))
					.Select(p => (p.Attributes & ParameterAttributes.HasDefault) != 0 ? p.DefaultValue : p.ParameterType.GetDefaultValue())
					.ToArray();
			}

			var ctorArgs =
				(settings.Args ?? Enumerable.Empty<object>())
				.Concat(new object[] { interceptor, mockMixinImpl })
				.Concat(settings.Mixins).ToArray();

			if (!mockConstructorCall)
			{
				if (proxyCtor != null)
				{
					return ProfilerInterceptor.GuardExternal(() => proxyCtor.Invoke(ctorArgs));
				}
				else
				{
					return ProfilerInterceptor.GuardExternal(() => Activator.CreateInstance(proxyType, ctorArgs));
				}
			}
			else
			{
				var result = UninitializedObjectFactory.Create(proxyType);
				proxyType.GetMethod(".init").Invoke(result, ctorArgs);
				return result;
			}
		}
Ejemplo n.º 3
0
        public void dynamic_proxy_interception()
        {
            var interceptor = new DynamicProxyInterceptor();
            var container = new Container(x => x.For<ITarget>()
                                                            .Use<Target>()
                                                            .Proxy((o, pg) => pg.CreateInterfaceProxyWithTarget((ITarget)o, interceptor)));

            container.Get<ITarget>().ToString();

            interceptor.Executed.Should().BeTrue();
        }
        public void TestSetUp()
        {
            var innerCatalog = new TypeCatalog(typeof(Customer));
            var interceptor = new FreezableInterceptor();
            interceptor.Freeze();

            var valueInterceptor = new DynamicProxyInterceptor(interceptor);
            var cfg = new InterceptionConfiguration()
                .AddInterceptor(valueInterceptor);

            var catalog = new InterceptingCatalog(innerCatalog, cfg);
            container = new CompositionContainer(catalog);
        }
Ejemplo n.º 5
0
		public static object CreateProxy(object wrappedInstance, MocksRepository repository, IMockMixin mockMixin)
		{
			var interceptor = new DynamicProxyInterceptor(repository);
			var realProxy = new MockingProxy((MarshalByRefObject)wrappedInstance, interceptor, mockMixin);
			return realProxy.GetTransparentProxy();
		}