コード例 #1
0
		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;
		}
コード例 #2
0
        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);
        }
コード例 #3
0
        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());
        }
コード例 #4
0
 public void TestRejectIncorrectGenericArgsToBinding()
 {
     ExpectError(() => {
         OpenGenericBinding.For(typeof(IDouble <,>)).To(typeof(MDouble <>));
     }, InjectorErrors.ErrorGenericsBindToTypeMustHaveSameTypeArgsAsKey, typeof(IDouble <,>), typeof(MDouble <>));
 }
コード例 #5
0
 public void TestRejectNonDerivedToBinding()
 {
     ExpectError(() => {
         OpenGenericBinding.For(typeof(Basic <>)).To(typeof(MImplicit <>));
     }, InjectorErrors.ErrorGenericsBindToTypeIsNotDerivedFromKey, typeof(Basic <>), typeof(MImplicit <>));
 }
コード例 #6
0
 public void TestRejectClosedGenericTypeParameter()
 {
     ExpectError(() => {
         OpenGenericBinding.For(typeof(Basic <object>));
     }, InjectorErrors.ErrorGenericsCannotCreateBindingForClosedGeneric, typeof(Basic <object>));
 }
コード例 #7
0
 public void TestRejectNonGenericTypeParameter()
 {
     ExpectError(() => {
         OpenGenericBinding.For(typeof(Exception));
     }, InjectorErrors.ErrorGenericsCannotCreateBindingForNonGeneric, typeof(Exception));
 }