コード例 #1
0
        /// <summary>
        /// Registers a new type to the <see cref="DependencyContainer"/>
        /// </summary>
        /// <typeparam name="TTypeToResolve">The type to resolve.</typeparam>
        /// <typeparam name="TResolvedType">The resolved type to return.</typeparam>
        /// <param name="options">The <see cref="LifeTimeOptions"/> for the type to register.</param>
        public void Register <TTypeToResolve, TResolvedType>(LifeTimeOptions options)
        {
            if (this.container.ContainsKey(typeof(TTypeToResolve)))
            {
                throw new Exception($"Type {typeof(TTypeToResolve).FullName} already registered.");
            }

            ResolvedTypeWithLifeTimeOptions targetType = new ResolvedTypeWithLifeTimeOptions(typeof(TResolvedType), options);

            this.container.Add(typeof(TTypeToResolve), targetType);
        }
コード例 #2
0
        /// <summary>
        /// Resolves the requested type from the <see cref="DependencyContainer"/>.
        /// </summary>
        /// <param name="typeToResolve">The <see cref="Type"/> to resolve.</param>
        /// <returns>The resolved <see cref="Type"/>.</returns>
        /// <exception cref="System.Exception">Type not registered.</exception>
        /// <exception cref="System.Exception">Unable to resolve a parameter for this <see cref="Type"/>.</exception>
        public object Resolve(Type typeToResolve, bool throwOnFailedParameterResolution = true)
        {
            if (!this.container.ContainsKey(typeToResolve))
            {
                throw new Exception($"Unable to resolve {typeToResolve.FullName}. Type is not registered.");
            }

            ResolvedTypeWithLifeTimeOptions resolvedType = this.container[typeToResolve];

            if (resolvedType.LifeTimeOption == LifeTimeOptions.ContainerControlled && resolvedType.InstanceValue != null)
            {
                return(resolvedType.InstanceValue);
            }

            ConstructorInfo constructorInfo = resolvedType.ResolvedType.GetConstructors().First();

            ParameterInfo[] paramsInfo     = constructorInfo.GetParameters();
            object[]        resolvedParams = new object[paramsInfo.Length];

            for (int x = 0; x < paramsInfo.Length; x++)
            {
                ParameterInfo param = paramsInfo[x];
                Type          t     = param.ParameterType;
                try
                {
                    object res = this.Resolve(t);
                    resolvedParams[x] = res;
                }
                catch (Exception ex)
                {
                    if (throwOnFailedParameterResolution)
                    {
                        throw new Exception($"Unable to resolve paramter type {t} for {typeToResolve}", ex);
                    }

                    resolvedParams[x] = null;
                    continue;
                }
            }

            object retOjbect = constructorInfo.Invoke(resolvedParams);

            if (resolvedType.LifeTimeOption == LifeTimeOptions.ContainerControlled)
            {
                resolvedType.InstanceValue = retOjbect;
            }

            return(retOjbect);
        }