/// <summary> /// Returns the <see cref="ITarget"/> that should be compiled as the body of the delegate. If <see cref="BoundTarget"/> /// is not null and its <see cref="ITarget.UseFallback"/> is false, then no binding is performed. /// /// Otherwise, a suitable target is sought from the <paramref name="compileContext"/>. If not found, then /// a <see cref="ResolvedTarget"/> is emitted with a fallback set to the <see cref="BoundTarget"/> if non-null, /// meaning that the delegate will actually perform a late-bound resolve operation on the container on which it's /// called. /// </summary> /// <param name="compileContext">The current compilation context, this is used to resolve the target to be bound.</param> /// <returns>The target to be compiled as the body of the factory.</returns> public ITarget Bind(ICompileContext compileContext) { if (BoundTarget != null && !BoundTarget.UseFallback) { return(BoundTarget); } return(compileContext.Fetch(ReturnType) ?? new ResolvedTarget(ReturnType, BoundTarget)); }
/// <summary> /// Attempts to obtain the target that this <see cref="ResolvedTarget"/> resolves to for the given <see cref="ICompileContext"/>. /// /// This function should be used by <see cref="ITargetCompiler"/> implementations when compiling factories which included this /// instance, who wish to perform some form of up-front optimisations. /// </summary> /// <param name="context">The context from which a target is to be resolved.</param> /// <returns>The target resolved by this target - could be the <see cref="FallbackTarget"/>, could be null.</returns> /// <remarks>The target that is returned depends both on the <paramref name="context"/> passed and also whether /// a <see cref="FallbackTarget"/> has been provided to this target. /// </remarks> public virtual ITarget Bind(ICompileContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } var fromContext = context.Fetch(DeclaredType); if (fromContext == null) { return(FallbackTarget); // might still be null of course } else if (fromContext.UseFallback) { return(FallbackTarget ?? fromContext); } return(fromContext); }