public LookupInProgressAdder(DiContainer container, Type concreteType) { if (DiContainer.LookupsInProgress.Contains(concreteType)) { throw new ZenjectResolveException( "Circular dependency detected! \nObject graph:\n" + DiContainer.GetCurrentObjectGraph()); } DiContainer.LookupsInProgress.Push(concreteType); _concreteType = concreteType; }
object ResolveFromType( ResolveContext context, object injectable, InjectableInfo injectInfo) { if (_container.HasBinding(injectInfo.ContractType, context)) { return(_container.Resolve(injectInfo.ContractType, context)); } // If it's a list it might map to a collection if (ReflectionUtil.IsGenericList(injectInfo.ContractType)) { var subType = injectInfo.ContractType.GetGenericArguments().Single(); return(_container.ResolveMany(subType, context, injectInfo.Optional)); } if (!injectInfo.Optional) { throw new ZenjectResolveException( "Unable to find field with type '{0}' when injecting dependencies into '{1}'. \nObject graph:\n {2}" .With(injectInfo.ContractType, injectable, _container.GetCurrentObjectGraph())); } return(null); }
public static IEnumerable <ZenjectResolveException> ValidateContract( DiContainer container, Type contractType, InjectContext context) { var matches = container.GetProviderMatches(contractType, context); if (matches.Count == 1) { foreach (var error in matches.Single().ValidateBinding(contractType, context)) { yield return(error); } } else { if (ReflectionUtil.IsGenericList(contractType)) { var subType = contractType.GetGenericArguments().Single(); matches = container.GetProviderMatches(subType, context); if (matches.IsEmpty()) { if (!context.Optional) { if (container.FallbackProvider != null) { foreach (var error in container.FallbackProvider.ValidateBinding(contractType, context)) { yield return(error); } } else { yield return(new ZenjectResolveException( "Could not find dependency with type 'List<{0}>'{1}. If the empty list is also valid, you can allow this by using the [InjectOptional] attribute.' \nObject graph:\n{2}" .With( subType.Name(), (context.EnclosingType == null ? "" : " when injecting into '{0}'".With(context.EnclosingType.Name())), DiContainer.GetCurrentObjectGraph()))); } } } else { foreach (var match in matches) { foreach (var error in match.ValidateBinding(contractType, context)) { yield return(error); } } } } else { if (!context.Optional) { if (matches.IsEmpty()) { if (container.FallbackProvider != null) { foreach (var error in container.FallbackProvider.ValidateBinding(contractType, context)) { yield return(error); } } else { yield return(new ZenjectResolveException( "Could not find required dependency with type '{0}'{1} \nObject graph:\n{2}" .With( contractType.Name(), (context.EnclosingType == null ? "" : " when injecting into '{0}'".With(context.EnclosingType.Name())), DiContainer.GetCurrentObjectGraph()))); } } else { yield return(new ZenjectResolveException( "Found multiple matches when only one was expected for dependency with type '{0}'{1} \nObject graph:\n{2}" .With( contractType.Name(), (context.EnclosingType == null ? "" : " when injecting into '{0}'".With(context.EnclosingType.Name())), DiContainer.GetCurrentObjectGraph()))); } } } } }
public static IEnumerable <ZenjectResolveException> ValidateObjectGraph( DiContainer container, Type concreteType, params Type[] extras) { using (container.PushLookup(concreteType)) { var typeInfo = TypeAnalyzer.GetInfo(concreteType); var extrasList = extras.ToList(); foreach (var dependInfo in typeInfo.AllInjectables) { Assert.IsEqual(dependInfo.EnclosingType, concreteType); if (TryTakingFromExtras(dependInfo.ContractType, extrasList)) { continue; } var context = new InjectContext( dependInfo, DiContainer.LookupsInProgress.ToList(), null); foreach (var error in ValidateContract( container, dependInfo.ContractType, context)) { yield return(error); } } if (!extrasList.IsEmpty()) { yield return(new ZenjectResolveException( "Found unnecessary extra parameters passed when injecting into '{0}' with types '{1}'. \nObject graph:\n{2}" .With(concreteType.Name(), String.Join(",", extrasList.Select(x => x.Name()).ToArray()), DiContainer.GetCurrentObjectGraph()))); } } }
public override object GetInstance(Type contractType, InjectContext context) { Assert.That(typeof(T).DerivesFromOrEqual(contractType)); var obj = _method(_container); Assert.That(obj != null, () => "Method provider returned null when looking up type '{0}'. \nObject graph:\n{1}".With(typeof(T).Name(), DiContainer.GetCurrentObjectGraph())); return(obj); }
public override object GetInstance(Type contractType, InjectContext context) { Assert.That(_componentType.DerivesFromOrEqual(contractType)); if (_instance == null) { if (_instantiator == null) { _instantiator = _container.Resolve <GameObjectInstantiator>(); } Assert.That(!_container.AllowNullBindings, "Tried to instantiate a MonoBehaviour with type '{0}' during validation. Object graph: {1}", _componentType, DiContainer.GetCurrentObjectGraph()); // We don't use the generic version here to avoid duplicate generic arguments to binder _instance = _instantiator.Instantiate(_componentType, _name); Assert.That(_instance != null); } return(_instance); }
internal static void Inject(DiContainer container, object injectable, IEnumerable <object> additional, bool shouldUseAll, ZenjectTypeInfo typeInfo) { Assert.IsEqual(typeInfo.TypeAnalyzed, injectable.GetType()); Assert.That(injectable != null); var additionalCopy = additional.ToList(); foreach (var injectInfo in typeInfo.FieldInjectables.Concat(typeInfo.PropertyInjectables)) { bool didInject = InjectFromExtras(injectInfo, injectable, additionalCopy); if (!didInject) { InjectFromResolve(injectInfo, container, injectable); } } if (shouldUseAll && !additionalCopy.IsEmpty()) { throw new ZenjectResolveException( "Passed unnecessary parameters when injecting into type '{0}'. \nExtra Parameters: {1}\nObject graph:\n{2}" .With(injectable.GetType().Name(), String.Join(",", additionalCopy.Select(x => x.GetType().Name()).ToArray()), DiContainer.GetCurrentObjectGraph())); } foreach (var methodInfo in typeInfo.PostInjectMethods) { using (ProfileBlock.Start("{0}.{1}()".With(injectable.GetType(), methodInfo.Name))) { methodInfo.Invoke(injectable, new object[0]); } } }
public LookupInProgressAdder(DiContainer container, Type concreteType) { if (DiContainer.LookupsInProgress.Contains(concreteType)) { Assert.That(false, () => "Circular dependency detected! \nObject graph:\n" + DiContainer.GetCurrentObjectGraph()); } DiContainer.LookupsInProgress.Push(concreteType); _concreteType = concreteType; }
public override object GetInstance(Type contractType, InjectContext context) { Assert.That(_componentType.DerivesFromOrEqual(contractType)); if (_instance == null) { Assert.That(!_container.AllowNullBindings, "Tried to instantiate a MonoBehaviour with type '{0}' during validation. Object graph: {1}", _componentType, DiContainer.GetCurrentObjectGraph()); _instance = _gameObject.AddComponent(_componentType); Assert.That(_instance != null); InjectionHelper.InjectMonoBehaviour(_container, _instance); } return(_instance); }
internal static void Inject( DiContainer container, object injectable, IEnumerable <TypeValuePair> extraArgMapParam, bool shouldUseAll, ZenjectTypeInfo typeInfo) { Assert.IsEqual(typeInfo.TypeAnalyzed, injectable.GetType()); Assert.That(injectable != null); // Make a copy since we remove from it below var extraArgMap = extraArgMapParam.ToList(); foreach (var injectInfo in typeInfo.FieldInjectables.Concat(typeInfo.PropertyInjectables)) { object value; if (InstantiateUtil.PopValueWithType(extraArgMap, injectInfo.ContractType, out value)) { injectInfo.Setter(injectable, value); } else { value = container.Resolve(injectInfo, injectable); if (injectInfo.Optional && value == null) { // Do not override in this case so it retains the hard-coded value } else { injectInfo.Setter(injectable, value); } } } if (shouldUseAll && !extraArgMap.IsEmpty()) { throw new ZenjectResolveException( "Passed unnecessary parameters when injecting into type '{0}'. \nExtra Parameters: {1}\nObject graph:\n{2}" .With(injectable.GetType().Name(), String.Join(",", extraArgMap.Select(x => x.Type.Name()).ToArray()), DiContainer.GetCurrentObjectGraph())); } foreach (var methodInfo in typeInfo.PostInjectMethods) { using (ProfileBlock.Start("{0}.{1}()", injectable.GetType(), methodInfo.Name)) { methodInfo.Invoke(injectable, new object[0]); } } }
public static void Inject(DiContainer container, object injectable, IEnumerable <object> additional, bool shouldUseAll) { Assert.That(injectable != null); var additionalCopy = additional.ToList(); var injectInfos = InjectablesFinder.GetFieldAndPropertyInjectables(injectable.GetType()); foreach (var injectInfo in injectInfos) { bool didInject = InjectFromExtras(injectInfo, injectable, additionalCopy); if (!didInject) { InjectFromResolve(injectInfo, container, injectable); } } if (shouldUseAll && !additionalCopy.IsEmpty()) { throw new ZenjectResolveException( "Passed unnecessary parameters when injecting into type '{0}'. \nExtra Parameters: {1}\nObject graph:\n{2}" .With(injectable.GetType().Name(), String.Join(",", additionalCopy.Select(x => x.GetType().Name()).ToArray()), container.GetCurrentObjectGraph())); } foreach (var methodInfo in InjectablesFinder.GetPostInjectMethods(injectable.GetType())) { methodInfo.Invoke(injectable, new object[0]); } }