Example #1
0
        /// <summary>
        /// Returns all exports that match the conditions of the specified import.
        /// </summary>
        /// <param name="definition">The <see cref="ImportDefinition"/> that defines the conditions of the
        /// <see cref="Export"/> to get.</param>
        /// <returns></returns>
        /// <result>
        /// An <see cref="IEnumerable{T}"/> of <see cref="Export"/> objects that match
        /// the conditions defined by <see cref="ImportDefinition"/>, if found; otherwise, an
        /// empty <see cref="IEnumerable{T}"/>.
        /// </result>
        /// <remarks>
        ///     <note type="inheritinfo">
        /// The implementers should not treat the cardinality-related mismatches as errors, and are not
        /// expected to throw exceptions in those cases.
        /// For instance, if the import requests exactly one export and the provider has no matching exports or more than one,
        /// it should return an empty <see cref="IEnumerable{T}"/> of <see cref="Export"/>.
        /// </note>
        /// </remarks>
        protected override IEnumerable <Export> GetExportsCore(ImportDefinition definition, AtomicComposition atomicComposition)
        {
            ThrowIfDisposed();

            IEnumerable <Export> exports = null;

            object source;

            if (!definition.Metadata.TryGetValue(CompositionConstants.ImportSourceMetadataName, out source))
            {
                source = ImportSource.Any;
            }

            switch ((ImportSource)source)
            {
            case ImportSource.Any:
                Assumes.NotNull(_rootProvider);
                _rootProvider.TryGetExports(definition, atomicComposition, out exports);
                break;

            case ImportSource.Local:
                Assumes.NotNull(_localExportProvider);
                _localExportProvider.TryGetExports(definition.RemoveImportSource(), atomicComposition, out exports);
                break;

            case ImportSource.NonLocal:
                if (_ancestorExportProvider != null)
                {
                    _ancestorExportProvider.TryGetExports(definition.RemoveImportSource(), atomicComposition, out exports);
                }
                break;
            }

            return(exports);
        }
Example #2
0
        /// <summary>
        /// Returns all exports that match the conditions of the specified import.
        /// </summary>
        /// <param name="definition">The <see cref="ImportDefinition"/> that defines the conditions of the
        /// <see cref="Export"/> to get.</param>
        /// <param name="atomicComposition">The transactional container for the composition.</param>
        /// <returns></returns>
        /// <result>
        /// An <see cref="IEnumerable{T}"/> of <see cref="Export"/> objects that match
        /// the conditions defined by <see cref="ImportDefinition"/>, if found; otherwise, an
        /// empty <see cref="IEnumerable{T}"/>.
        /// </result>
        /// <remarks>
        /// <note type="inheritinfo">
        /// The implementers should not treat the cardinality-related mismatches as errors, and are not
        /// expected to throw exceptions in those cases.
        /// For instance, if the import requests exactly one export and the provider has no matching exports or more than one,
        /// it should return an empty <see cref="IEnumerable{T}"/> of <see cref="Export"/>.
        /// </note>
        /// </remarks>
        protected override IEnumerable <Export>?GetExportsCore(ImportDefinition definition, AtomicComposition?atomicComposition)
        {
            ThrowIfDisposed();

            IEnumerable <Export>?exports = null;

            if (!definition.Metadata.TryGetValue(CompositionConstants.ImportSourceMetadataName, out object?source))
            {
                source = ImportSource.Any;
            }

            switch ((ImportSource)source !)
            {
            case ImportSource.Any:
                if (_rootProvider == null)
                {
                    throw new Exception(SR.Diagnostic_InternalExceptionMessage);
                }
                _rootProvider.TryGetExports(definition, atomicComposition, out exports);
                break;

            case ImportSource.Local:
                if (_localExportProvider == null)
                {
                    throw new Exception(SR.Diagnostic_InternalExceptionMessage);
                }
                _localExportProvider.TryGetExports(definition.RemoveImportSource(), atomicComposition, out exports);
                break;

            case ImportSource.NonLocal:
                if (_ancestorExportProvider != null)
                {
                    _ancestorExportProvider.TryGetExports(definition.RemoveImportSource(), atomicComposition, out exports);
                }
                break;
            }

            return(exports);
        }