/// <summary> /// Resolves the binding. /// </summary> /// <param name="binding">Binding to be resolved.</param> /// <param name="type">Binding type.</param> /// <param name="member">Member for which the binding is being resolved.</param> /// <param name="parentInstance">Parent object in which the resolve is occuring.</param> /// <param name="identifier">The binding identifier to be looked for.</param> /// <returns>The resolved instance from the binding.</returns> protected object ResolveBinding(BindingInfo binding, Type type, InjectionMember member, object parentInstance, object identifier) { //Condition evaluation. if (binding.condition != null) { var context = new InjectionContext() { member = member, memberType = type, identifier = identifier, parentType = (parentInstance != null ? parentInstance.GetType() : null), parentInstance = parentInstance, injectType = binding.type }; if (!binding.condition(context)) { return(null); } } //Identifier evaluation. bool resolveByIdentifier = identifier != null; bool bindingHasIdentifier = binding.identifier != null; if ((!resolveByIdentifier && bindingHasIdentifier) || (resolveByIdentifier && !bindingHasIdentifier) || (resolveByIdentifier && bindingHasIdentifier && !binding.identifier.Equals(identifier))) { return(null); } //Instance evaluation. object instance = null; if (this.bindingEvaluation != null) { var delegates = this.bindingEvaluation.GetInvocationList(); for (int delegateIndex = 0; delegateIndex < delegates.Length; delegateIndex++) { instance = ((BindingEvaluationHandler)delegates[delegateIndex]).Invoke(this, ref binding); } } if (instance == null) { if (binding.instanceType == BindingInstance.Transient) { instance = this.Instantiate(binding.value as Type); } else if (binding.instanceType == BindingInstance.Factory) { var context = new InjectionContext() { member = member, memberType = type, identifier = identifier, parentType = (parentInstance != null ? parentInstance.GetType() : null), parentInstance = parentInstance, injectType = binding.type }; instance = (binding.value as IFactory).Create(context); } else { //Binding is a singleton object. //If the binding value is a type, instantiate it. if (binding.value is Type) { binding.value = this.Instantiate(binding.value as Type); } instance = binding.value; } } if (this.bindingResolution != null) { this.bindingResolution(this, ref binding, ref instance); } return(instance); }