コード例 #1
0
        public Registration(TypeKey typeKey, Lifetime lifeTime, TypeConstructionInfo typeConstructionInfo = null, IReadOnlyList <Type> mappedAbstractionTypes = null, bool isTrackingDisposables = true)
        {
            TypeKey  = typeKey.MustBeValidRegistrationTypeKey();
            Lifetime = lifeTime.MustNotBeNull(nameof(lifeTime));
            if (lifeTime.IsCreatingNewInstances)
            {
                if (typeConstructionInfo == null)
                {
                    throw new RegistrationException($"The Type Construction Info for {typeKey} must not be null when the Lifetime \"{lifeTime}\" of the registration is able to create new instances of the target type.");
                }
                if (typeConstructionInfo.TypeKey != typeKey)
                {
                    throw new RegistrationException($"The Type Key {typeConstructionInfo.TypeKey} of the Type Construction Info is not equal to the Type Key {typeKey} of the registration.");
                }
                TypeConstructionInfo = typeConstructionInfo;
            }
            else if (TargetType.IsGenericTypeDefinition())
            {
                throw new RegistrationException($"You cannot register the generic type definition \"{TargetType}\" when the corresponding lifetime does not create instances.");
            }

            if (mappedAbstractionTypes.IsNullOrEmpty() == false)
            {
                // ReSharper disable once PossibleNullReferenceException
                for (var i = 0; i < mappedAbstractionTypes.Count; i++)
                {
                    mappedAbstractionTypes[i].MustBeBaseTypeOf(TargetType);
                }
            }
            MappedAbstractionTypes = mappedAbstractionTypes;
            IsTrackingDisposables  = isTrackingDisposables;
        }