public override object BuildUp(IBuilderContext context, object buildKey, object existing) { ICreationPolicy creationPolicy = context.Policies.Get <ICreationPolicy>(buildKey); IInterfaceInterceptionPolicy interceptionPolicy = context.Policies.Get <IInterfaceInterceptionPolicy>(buildKey); Type typeToBuild; if (creationPolicy != null && creationPolicy.SupportsReflection && interceptionPolicy != null && TryGetTypeFromBuildKey(buildKey, out typeToBuild)) { ConstructorInfo ctor = creationPolicy.GetConstructor(context, buildKey); object[] ctorParams = creationPolicy.GetParameters(context, ctor); Type originalType; if (!TryGetTypeFromBuildKey(context.OriginalBuildKey, out originalType)) { originalType = typeToBuild; } buildKey = InterceptInterface(context, typeToBuild, originalType, interceptionPolicy, ctor, ctorParams); } return(base.BuildUp(context, buildKey, existing)); }
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]); }
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]); }
public void ReflectShouldHappenOnGenericBaseClass() { PolicyList policies = new PolicyList(); MethodBase method = typeof(IGeneric <>).GetMethod("DoSomething"); InterceptionReflector.Reflect <IGeneric <int>, Generic <int> >(policies, new StubObjectFactory()); IInterfaceInterceptionPolicy policy = policies.Get <IInterfaceInterceptionPolicy>(typeof(Generic <>)); Assert.Equal(1, policy.Count); Assert.Equal(1, policy[method].Count); Assert.IsType <RecordingHandler>(policy[method][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]); }
public void TwoInterceptedMethods() { PolicyList policies = new PolicyList(); MethodBase method1 = typeof(ITwoMethods).GetMethod("InterceptedMethod1"); MethodBase method2 = typeof(ITwoMethods).GetMethod("InterceptedMethod2"); InterceptionReflector.Reflect <ITwoMethods, TwoMethods>(policies, new StubObjectFactory()); IInterfaceInterceptionPolicy policy = policies.Get <IInterfaceInterceptionPolicy>(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]); }