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
        private IResolver BindImplicit(BindingKey bindingKey)
        {
            lock (syncLock) {
                IResolver resolver;
                if (allResolvers.TryGetValue(bindingKey, out resolver))
                {
                    return(resolver);
                }

                Type implType = BindingAttributeUtils.GetImplementedBy(bindingKey.BindingType);

                return(CreateResolverInstanceGeneric(
                           bindingKey.ToImplicit(),
                           (implType != null) ? implType : bindingKey.BindingType));
            }
        }
示例#3
0
        public void TestToImplicit_Equals_HashCode()
        {
            BindingKey b1q  = BindingKey.Get(typeof(object), "foo");
            BindingKey b2q  = BindingKey.Get <object>("foo");
            BindingKey b1qI = b1q.ToImplicit();
            BindingKey b2qI = b2q.ToImplicit();

            Assert.AreEqual(false, b1qI.IsMember);
            Assert.AreEqual(false, b2qI.IsMember);
            Assert.AreEqual("foo", b1qI.Qualifier);
            Assert.AreEqual("foo", b2qI.Qualifier);

            Assert.AreNotSame(b1qI, b2qI);
            Assert.AreSame(b1qI, b1qI.ToImplicit());               // should return reference to self

            Assert.IsFalse(b1q.IsImplicit);
            Assert.IsTrue(b2qI.IsImplicit);
            Assert.IsTrue(b2qI.IsImplicit);

            Assert.AreEqual(b1qI, b2qI);
            Assert.AreEqual(b1qI.GetHashCode(), b2qI.GetHashCode());
            Assert.AreEqual(b1q, b1qI);
        }