Example #1
0
        public override object GetInstance(InjectionCore injectionCore, ProviderContext context)
        {
            if (this.singletonInstance == null)
            {
                this.singletonInstance = base.GetInstance(injectionCore, context);
            }

            return(this.singletonInstance);
        }
        public override ISet <T> GetInstance <T>(InjectionCore injectionCore, ProviderContext context)
        {
            HashSet <T> instanceList = new HashSet <T>();

            foreach (InjectionProvider provider in this.providerList)
            {
                instanceList.Add((T)provider.GetInstance(injectionCore, context));
            }

            return(instanceList);
        }
Example #3
0
        public virtual object GetInstance(InjectionCore injectionCore, ProviderContext context)
        {
            if (context == null)
            {
                context = new ProviderContext();
            }

            ParameterInfo[] parameterInfoList = this.providerMethod.GetParameters();
            var             parameters        = new object[parameterInfoList.Length];

            for (int i = 0; i < parameterInfoList.Length; i++)
            {
                NamedAttribute namedAttribute = parameterInfoList[i].GetCustomAttribute <NamedAttribute>();
                if (namedAttribute == null)
                {
                    if (context.TypedDependencies.Contains(parameterInfoList[i].ParameterType))
                    {
                        throw new InvalidOperationException(
                                  $"Dependency loop detected when providing dependency type {parameterInfoList[i].ParameterType.Name}.");
                    }

                    context.TypedDependencies.Add(parameterInfoList[i].ParameterType);
                    parameters[i] = injectionCore.Get(parameterInfoList[i].ParameterType, context);
                    context.TypedDependencies.Remove(parameterInfoList[i].ParameterType);
                }
                else
                {
                    if (context.NamedDependencies.Contains(namedAttribute.Name))
                    {
                        throw new InvalidOperationException(
                                  $"Dependency loop detected when providing named dependency '{namedAttribute.Name}'.");
                    }

                    context.NamedDependencies.Add(namedAttribute.Name);
                    parameters[i] = injectionCore.Get(namedAttribute.Name, context);
                    context.NamedDependencies.Remove(namedAttribute.Name);
                }
            }

            ConstructorInfo constructor = this.providerMethod as ConstructorInfo;

            return(constructor != null
                ? constructor.Invoke(parameters)
                : this.providerMethod.Invoke(null, parameters));
        }