static InjectableInfo CreateForMember(MemberInfo memInfo, Type enclosingType) { var injectAttr = memInfo.Attribute <InjectAttribute>(); var info = new InjectableInfo() { Optional = memInfo.HasAttribute(typeof(InjectOptionalAttribute)), Identifier = (injectAttr == null ? null : injectAttr.Identifier), SourceName = memInfo.Name, EnclosingType = enclosingType, }; if (memInfo is FieldInfo) { var fieldInfo = (FieldInfo)memInfo; info.Setter = ((object injectable, object value) => fieldInfo.SetValue(injectable, value)); info.ContractType = fieldInfo.FieldType; } else { Assert.That(memInfo is PropertyInfo); var propInfo = (PropertyInfo)memInfo; info.Setter = ((object injectable, object value) => propInfo.SetValue(injectable, value, null)); info.ContractType = propInfo.PropertyType; } return(info); }
static InjectableInfo CreateForMember(MemberInfo memInfo, Type enclosingType) { var injectAttr = memInfo.Attribute<InjectAttribute>(); var info = new InjectableInfo() { Optional = memInfo.HasAttribute(typeof(InjectOptionalAttribute)), Identifier = (injectAttr == null ? null : injectAttr.Identifier), SourceName = memInfo.Name, EnclosingType = enclosingType, }; if (memInfo is FieldInfo) { var fieldInfo = (FieldInfo)memInfo; info.Setter = ((object injectable, object value) => fieldInfo.SetValue(injectable, value)); info.ContractType = fieldInfo.FieldType; } else { Assert.That(memInfo is PropertyInfo); var propInfo = (PropertyInfo)memInfo; info.Setter = ((object injectable, object value) => propInfo.SetValue(injectable, value, null)); info.ContractType = propInfo.PropertyType; } return info; }
internal object Resolve( InjectableInfo injectInfo, object targetInstance) { var context = new ResolveContext( injectInfo, LookupsInProgress.ToList(), targetInstance); return(Resolve(injectInfo.ContractType, context, injectInfo.Optional)); }
internal ResolveContext( InjectableInfo injectInfo, List <Type> parents, object targetInstance) { Identifier = injectInfo.Identifier; SourceName = injectInfo.SourceName; EnclosingType = injectInfo.EnclosingType; EnclosingInstance = targetInstance; ParentTypes = parents; }
internal InjectContext( InjectableInfo injectInfo, List<Type> parents, object targetInstance) { Optional = injectInfo.Optional; Identifier = injectInfo.Identifier; SourceName = injectInfo.SourceName; EnclosingType = injectInfo.EnclosingType; EnclosingInstance = targetInstance; ParentTypes = parents; }
internal InjectContext( DiContainer container, InjectableInfo injectInfo, List <Type> parents, object targetInstance) { Container = container; Optional = injectInfo.Optional; Identifier = injectInfo.Identifier; SourceName = injectInfo.SourceName; EnclosingType = injectInfo.EnclosingType; EnclosingInstance = targetInstance; ParentTypes = parents; }
static void InjectFromResolve( InjectableInfo injectInfo, DiContainer container, object targetInstance) { var valueObj = container.Resolve(injectInfo, targetInstance); if (valueObj == null && !container.AllowNullBindings) { // Do not change if optional // Since it may have some hard coded value Assert.That(injectInfo.Optional); // Should have thrown resolve exception otherwise } else { Assert.IsNotNull(injectInfo.Setter); injectInfo.Setter(targetInstance, valueObj); } }
static bool InjectFromExtras( InjectableInfo injectInfo, object injectable, List<object> additional) { foreach (object obj in additional) { if (injectInfo.ContractType.IsAssignableFrom(obj.GetType())) { Assert.IsNotNull(injectInfo.Setter); injectInfo.Setter(injectable, obj); additional.Remove(obj); return true; } } return false; }
static bool InjectFromExtras( InjectableInfo injectInfo, object injectable, List <object> additional) { foreach (object obj in additional) { if (injectInfo.ContractType.IsAssignableFrom(obj.GetType())) { Assert.IsNotNull(injectInfo.Setter); injectInfo.Setter(injectable, obj); additional.Remove(obj); return(true); } } return(false); }
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); }
internal object Resolve(InjectableInfo injectInfo) { return(Resolve(injectInfo, null)); }