public void SelectConstructorWith2Parameters() { ConstructorInfo ctor = typeof(ClassWithConstructorParameters).GetConstructor(Types(typeof(int), typeof(string))); var policy = new SpecifiedConstructorSelectorPolicy(ctor, new InjectionParameterValue[] { new InjectionParameter <int>(37), new InjectionParameter <string>("abc") }); var builderContext = new BuilderContextMock(new NamedTypeBuildKey(typeof(ClassWithConstructorParameters))); SelectedConstructor selectedCtor = policy.SelectConstructor(builderContext, builderContext.PersistentPolicies); Assert.AreEqual(ctor, selectedCtor.Constructor); Assert.AreEqual(2, selectedCtor.GetParameterKeys().Length); string[] keys = selectedCtor.GetParameterKeys(); Assert.AreEqual(2, keys.Length); foreach (string key in keys) { AssertPolicyIsCorrect(key, builderContext); } }
public void InjectionConstructorInsertsChooserForConstructorWithParameters() { string expectedString = "Hello"; int expectedInt = 12; var ctor = new InjectionConstructor(expectedString, expectedInt); var context = new MockBuilderContext { BuildKey = new NamedTypeBuildKey(typeof(GuineaPig)) }; IPolicyList policies = context.PersistentPolicies; ctor.AddPolicies(typeof(GuineaPig), policies); var selector = policies.Get <IConstructorSelectorPolicy>( new NamedTypeBuildKey(typeof(GuineaPig))); SelectedConstructor selected = selector.SelectConstructor(context, policies); string[] keys = selected.GetParameterKeys(); Assert.AreEqual(typeof(GuineaPig).GetConstructor(Sequence.Collect(typeof(string), typeof(int))), selected.Constructor); Assert.AreEqual(2, keys.Length); Assert.AreEqual(expectedString, (string)ResolveValue(policies, keys[0])); Assert.AreEqual(expectedInt, (int)ResolveValue(policies, keys[1])); }
public void InjectionConstructorSetsResolverForInterfaceToLookupInContainer() { InjectionConstructor ctor = new InjectionConstructor("Logger", typeof(ILogger)); TestingBuilderContext context = new TestingBuilderContext(); context.BuildKey = typeof(GuineaPig); IPolicyList policies = context.PersistentPolicies; ctor.AddPolicies(typeof(GuineaPig), policies); IConstructorSelectorPolicy selector = policies.Get <IConstructorSelectorPolicy>( new NamedTypeBuildKey(typeof(GuineaPig))); SelectedConstructor selected = selector.SelectConstructor(context); string[] keys = selected.GetParameterKeys(); Assert.AreEqual(typeof(GuineaPig).GetConstructor(Sequence.Collect(typeof(string), typeof(ILogger))), selected.Constructor); Assert.AreEqual(2, keys.Length); IDependencyResolverPolicy policy = context.Policies.Get <IDependencyResolverPolicy>(keys[1]); Assert.IsTrue(policy is NamedTypeDependencyResolverPolicy); }
public void SelectConstructorWithNoParameters() { ConstructorInfo ctor = typeof(ClassWithSimpleConstructor).GetConstructor(new Type[0]); var policy = new SpecifiedConstructorSelectorPolicy(ctor, new InjectionParameterValue[0]); var builderContext = new BuilderContextMock(new NamedTypeBuildKey(typeof(ClassWithSimpleConstructor))); SelectedConstructor selectedCtor = policy.SelectConstructor(builderContext, new PolicyList()); Assert.AreEqual(ctor, selectedCtor.Constructor); Assert.AreEqual(0, selectedCtor.GetParameterKeys().Length); }
private static SelectedConstructor FindNewConstructor(SelectedConstructor originalConstructor, Type interceptingType) { var originalParams = originalConstructor.Constructor.GetParameters(); var newConstructorInfo = interceptingType.GetConstructor(originalParams.Select(pi => pi.ParameterType).ToArray()); var newConstructor = new SelectedConstructor(newConstructorInfo); foreach (var key in originalConstructor.GetParameterKeys()) { newConstructor.AddParameterKey(key); } return newConstructor; }
private static SelectedConstructor FindNewConstructor(SelectedConstructor originalConstructor, Type interceptingType) { ParameterInfo[] originalParams = originalConstructor.Constructor.GetParameters(); ConstructorInfo newConstructorInfo = interceptingType.GetConstructor(originalParams.Select(pi => pi.ParameterType).ToArray()); SelectedConstructor newConstructor = new SelectedConstructor(newConstructorInfo); foreach (string key in originalConstructor.GetParameterKeys()) { newConstructor.AddParameterKey(key); } return(newConstructor); }
private static SelectedConstructor FindNewConstructor(SelectedConstructor originalConstructor, Type proxyType) { ParameterInfo[] originalParams = originalConstructor.Constructor.GetParameters(); ConstructorInfo newConstructorInfo = proxyType.GetConstructor(Seq.Make(originalParams) .Map <Type>(delegate(ParameterInfo pi) { return(pi.ParameterType); }).ToArray()); SelectedConstructor newConstructor = new SelectedConstructor(newConstructorInfo); foreach (string key in originalConstructor.GetParameterKeys()) { newConstructor.AddParameterKey(key); } return(newConstructor); }
public void InjectionConstructorInsertsChooserForDefaultConstructor() { InjectionConstructor ctor = new InjectionConstructor(); TestingBuilderContext context = new TestingBuilderContext(); IPolicyList policies = context.PersistentPolicies; ctor.AddPolicies(typeof(GuineaPig), policies); IConstructorSelectorPolicy selector = policies.Get <IConstructorSelectorPolicy>( new NamedTypeBuildKey(typeof(GuineaPig))); SelectedConstructor selected = selector.SelectConstructor(context); Assert.AreEqual(typeof(GuineaPig).GetConstructor(new Type[0]), selected.Constructor); Assert.AreEqual(0, selected.GetParameterKeys().Length); }
private IEnumerable<Expression> BuildConstructionParameterExpressions(DynamicBuildPlanGenerationContext buildContext, SelectedConstructor selectedConstructor, string constructorSignature) { int i = 0; var constructionParameters = selectedConstructor.Constructor.GetParameters(); foreach (string parameterKey in selectedConstructor.GetParameterKeys()) { yield return buildContext.CreateParameterExpression( parameterKey, constructionParameters[i].ParameterType, Expression.Call(null, setCurrentOperationToResolvingParameter, Expression.Constant(constructionParameters[i].Name, typeof(string)), Expression.Constant(constructorSignature), buildContext.ContextParameter ) ); i++; } }