public override object BuildUp(IBuilderContext context,
                                       object buildKey,
                                       object existing)
        {
            Type typeToBuild;

            if (TryGetTypeFromBuildKey(buildKey, out typeToBuild))
            {
                Type originalType;

                if (!TryGetTypeFromBuildKey(context.OriginalBuildKey, out originalType))
                {
                    originalType = typeToBuild;
                }

                IObjectFactory factory = context.Locator.Get <IObjectFactory>();

                if (factory != null)
                {
                    InterceptionReflector.Reflect(originalType, typeToBuild, context.Policies, factory);
                }
            }

            return(base.BuildUp(context, buildKey, existing));
        }
Пример #2
0
            public void ClassWithNoDecorations()
            {
                PolicyList policies = new PolicyList();

                InterceptionReflector.Reflect <object>(policies, new StubObjectFactory());

                Assert.Equal(0, policies.Count);
            }
Пример #3
0
            public void RequestingToBuildNonInterfaceMeansMethodsNotIntercepted()
            {
                PolicyList policies = new PolicyList();

                InterceptionReflector.Reflect <OneMethod>(policies, new StubObjectFactory());

                Assert.Equal(0, policies.Count);
            }
Пример #4
0
            public void NonPublicInterfaceNotCompatible()
            {
                PolicyList policies = new PolicyList();

                Assert.Throws <InvalidOperationException>(
                    delegate
                {
                    InterceptionReflector.Reflect <INonPublicInterface, NonPublicInterface>(policies, new StubObjectFactory());
                });
            }
Пример #5
0
            public void VirtualSealedMethodNotCompatible()
            {
                PolicyList policies = new PolicyList();

                Assert.Throws <InvalidOperationException>(
                    delegate
                {
                    InterceptionReflector.Reflect <VirtualSealed>(policies, new StubObjectFactory());
                });
            }
Пример #6
0
            public void SealedTypeNotCompatible()
            {
                PolicyList policies = new PolicyList();

                Assert.Throws <InvalidOperationException>(
                    delegate
                {
                    InterceptionReflector.Reflect <SealedClass>(policies, new StubObjectFactory());
                });
            }
Пример #7
0
            public void NonMBROTypeIncompatibleWithRemoting()
            {
                PolicyList policies = new PolicyList();

                Assert.Throws <InvalidOperationException>(
                    delegate
                {
                    InterceptionReflector.Reflect <NonMBRO>(policies, new StubObjectFactory());
                });
            }
Пример #8
0
            public void CanMixInterceptionTypes()
            {
                PolicyList policies = new PolicyList();

                InterceptionReflector.Reflect <IFoo, FooBar>(policies, new StubObjectFactory());

                Assert.Equal(1, policies.Get <IVirtualInterceptionPolicy>(typeof(FooBar)).Count);
                Assert.Equal(1, policies.Get <IInterfaceInterceptionPolicy>(typeof(FooBar)).Count);
                Assert.Equal(1, policies.Get <IRemotingInterceptionPolicy>(typeof(FooBar)).Count);
            }
Пример #9
0
            public void OneInterceptedMethod()
            {
                PolicyList policies = new PolicyList();
                MethodBase method   = typeof(IOneMethod).GetMethod("InterceptedMethod");

                InterceptionReflector.Reflect <IOneMethod, OneMethod>(policies, new StubObjectFactory());
                IInterfaceInterceptionPolicy policy = policies.Get <IInterfaceInterceptionPolicy>(typeof(OneMethod));

                Assert.Equal(1, policy.Count);
                Assert.Equal(1, policy[method].Count);
                Assert.IsType <RecordingHandler>(policy[method][0]);
            }
Пример #10
0
            public void ReflectShouldHappenOnGenericBaseClass()
            {
                PolicyList policies = new PolicyList();
                MethodBase method   = typeof(Generic <>).GetMethod("DoSomething");

                InterceptionReflector.Reflect <Generic <int> >(policies, new StubObjectFactory());
                IVirtualInterceptionPolicy policy = policies.Get <IVirtualInterceptionPolicy>(typeof(Generic <>));

                Assert.Equal(1, policy.Count);
                Assert.Equal(1, policy[method].Count);
                Assert.IsType <RecordingHandler>(policy[method][0]);
            }
Пример #11
0
            public void InterceptionIsInherited()
            {
                PolicyList policies = new PolicyList();
                MethodBase method   = typeof(DerivedOneMethod).GetMethod("InterceptedMethod");

                InterceptionReflector.Reflect <DerivedOneMethod>(policies, new StubObjectFactory());
                IRemotingInterceptionPolicy policy = policies.Get <IRemotingInterceptionPolicy>(typeof(DerivedOneMethod));

                Assert.Equal(1, policy.Count);
                Assert.Equal(1, policy[method].Count);
                Assert.IsType <RecordingHandler>(policy[method][0]);
            }
Пример #12
0
            public void RequestingToBuildInterface1WillNotInterceptedInterface2Methods()
            {
                PolicyList policies = new PolicyList();
                MethodBase method   = typeof(IInterface1).GetMethod("InterceptedMethod1");

                InterceptionReflector.Reflect <IInterface1, TwoInterfaceClass>(policies, new StubObjectFactory());
                IInterfaceInterceptionPolicy policy = policies.Get <IInterfaceInterceptionPolicy>(typeof(TwoInterfaceClass));

                Assert.Equal(1, policy.Count);
                Assert.Equal(1, policy[method].Count);
                Assert.IsType <RecordingHandler>(policy[method][0]);
            }
Пример #13
0
            public void CanAddHandlersInInheritedClass()
            {
                PolicyList policies = new PolicyList();
                MethodBase method   = typeof(IOneMethod).GetMethod("InterceptedMethod");

                InterceptionReflector.Reflect <IOneMethod, DerivedWithAddedIntercepts>(policies, new StubObjectFactory());
                IInterfaceInterceptionPolicy policy = policies.Get <IInterfaceInterceptionPolicy>(typeof(DerivedWithAddedIntercepts));

                Assert.Equal(1, policy.Count);
                Assert.Equal(2, policy[method].Count);
                Assert.IsType <RecordingHandler>(policy[method][0]);
                Assert.IsType <RecordingHandler>(policy[method][1]);
            }
Пример #14
0
            public void TwoAttributesOnOneMethod()
            {
                PolicyList policies = new PolicyList();
                MethodBase method   = typeof(OneMethodTwoAttributes).GetMethod("InterceptedMethod");

                InterceptionReflector.Reflect <OneMethodTwoAttributes>(policies, new StubObjectFactory());
                IVirtualInterceptionPolicy policy = policies.Get <IVirtualInterceptionPolicy>(typeof(OneMethodTwoAttributes));

                Assert.Equal(1, policy.Count);
                Assert.Equal(2, policy[method].Count);
                Assert.IsType <RecordingHandler>(policy[method][0]);
                Assert.IsType <RecordingHandler>(policy[method][1]);
            }
Пример #15
0
            public void TwoInterceptedMethods()
            {
                PolicyList policies = new PolicyList();
                MethodBase method1  = typeof(TwoMethods).GetMethod("InterceptedMethod1");
                MethodBase method2  = typeof(TwoMethods).GetMethod("InterceptedMethod2");

                InterceptionReflector.Reflect <TwoMethods>(policies, new StubObjectFactory());
                IVirtualInterceptionPolicy policy = policies.Get <IVirtualInterceptionPolicy>(typeof(TwoMethods));

                Assert.Equal(2, policy.Count);
                Assert.Equal(1, policy[method1].Count);
                Assert.IsType <RecordingHandler>(policy[method1][0]);
                Assert.Equal(1, policy[method2].Count);
                Assert.IsType <RecordingHandler>(policy[method2][0]);
            }