private BindingKey ResolveBindingForGeneric(BindingKey explicitKey, Type bindingType) { var genericBindingKey = BindingKey.Get (bindingType.GetGenericTypeDefinition (), explicitKey.Qualifier); var genericBindingType = bindingType.GetGenericTypeDefinition (); var genericTypeArguments = bindingType.GetGenericArguments (); IBindingConfig genericBindingConfig; Type genericConcreteType = GetGenericImplementation (genericBindingKey, genericBindingType, out genericBindingConfig); // Have 'implementedBy OR explicit binding' if (genericConcreteType != null) { OpenGenericBinding.For (genericBindingType).To (genericConcreteType); // validate binding Type concreteType = genericConcreteType.MakeGenericType (genericTypeArguments); var binding = new GenericBinding () { BindingConfig = new BindingConfig(concreteType), BindingKey = explicitKey.ToImplicit(), ConcreteType = concreteType }; if (genericBindingConfig != null) { binding.BindingConfig.Lifestyle = genericBindingConfig.Lifestyle; } injector.Register (binding); return binding.BindingKey; } return null; }
public void TestBasicRegistrationWithLifestyles() { var injector = new Injector(); injector.Register(OpenGenericBinding.For(typeof(IBasic <>)).To(typeof(Basic <>)).SetLifestyle(Lifestyle.Singleton)); var obj = injector.Resolve <IBasic <object> > (); Assert.NotNull(obj); Assert.AreEqual(typeof(Basic <object>), obj.GetType()); var obj2 = injector.Resolve <IBasic <object> > (); Assert.AreSame(obj, obj2); }
public void TestBasicRegistration() { var injector = new Injector(); injector.Register(OpenGenericBinding.For(typeof(IBasic <>)).To(typeof(Basic <>))); var obj = injector.Resolve <IBasic <object> > (); Assert.NotNull(obj); Assert.AreEqual(typeof(Basic <object>), obj.GetType()); var obj2 = injector.Resolve <IBasic <object> > (); Assert.AreNotSame(obj, obj2); var ex = injector.Resolve <IBasic <Exception> > (); Assert.NotNull(ex); Assert.AreEqual(typeof(Basic <Exception>), ex.GetType()); }
public void TestRejectIncorrectGenericArgsToBinding() { ExpectError(() => { OpenGenericBinding.For(typeof(IDouble <,>)).To(typeof(MDouble <>)); }, InjectorErrors.ErrorGenericsBindToTypeMustHaveSameTypeArgsAsKey, typeof(IDouble <,>), typeof(MDouble <>)); }
public void TestRejectNonDerivedToBinding() { ExpectError(() => { OpenGenericBinding.For(typeof(Basic <>)).To(typeof(MImplicit <>)); }, InjectorErrors.ErrorGenericsBindToTypeIsNotDerivedFromKey, typeof(Basic <>), typeof(MImplicit <>)); }
public void TestRejectClosedGenericTypeParameter() { ExpectError(() => { OpenGenericBinding.For(typeof(Basic <object>)); }, InjectorErrors.ErrorGenericsCannotCreateBindingForClosedGeneric, typeof(Basic <object>)); }
public void TestRejectNonGenericTypeParameter() { ExpectError(() => { OpenGenericBinding.For(typeof(Exception)); }, InjectorErrors.ErrorGenericsCannotCreateBindingForNonGeneric, typeof(Exception)); }