Exemple #1
0
        /// <summary>
        /// Convenience constructor method that creates a new <see cref="MefSingletonComposer"/>, that is
        /// created from a new <see cref="ConventionBuilder"/>, for each given <c>exportTypes</c>,
        /// and each is <c>Shared</c>. The returned object composes only for the given types.
        /// Notice also, this class extends <see cref="Composer{TTarget}"/>, and this by
        /// default sets <see cref="Composer{TTarget}.ComposeExceptionPolicy"/> to
        /// <see cref="ComposerExceptionPolicy.ThrowNone"/>.
        /// </summary>
        /// <param name="exportTypes">The shared export types for the composer.</param>
        /// <param name="onlyDerivedTypesExclusively">Allows you to specify only
        /// <see cref="ConventionBuilder.ForTypesDerivedFrom"/> (if true), only
        /// <see cref="ConventionBuilder.ForType"/> (if false), or both (if null).
        /// If the type is abstract then ForType is not applied.</param>
        /// <param name="disposeCompositionHostsWithThis">Specifies how all created
        /// <see cref="CompositionHost"/> instances are handled when this object is disposed: notice
        /// that this defaults to true --- when disposed, the contained exports may be disposed. If
        /// not disposed, the references are simply dropped.</param>
        /// <param name="disposeProvidersWithThis">Specifies how added PROVIDERS are handled when
        /// this object is disposed: notice that this defaults to true.</param>s
        /// <param name="handleNewExports">Optional argument for the result.</param>
        /// <returns>Not null.</returns>
        public static MefSingletonComposer ForTypes(
            IEnumerable <Type> exportTypes,
            bool?onlyDerivedTypesExclusively,
            bool disposeCompositionHostsWithThis = true,
            bool disposeProvidersWithThis        = true,
            Func <List <object>, IReadOnlyList <object>, Action> handleNewExports = null)
        {
            if (exportTypes == null)
            {
                throw new ArgumentNullException(nameof(exportTypes));
            }
            Type[]            exportTypesArray = exportTypes.ToArray();
            ConventionBuilder conventions      = new ConventionBuilder();

            foreach (Type exportType in exportTypesArray)
            {
                conventions.ApplyConventions(exportType, onlyDerivedTypesExclusively);
            }
            return(new MefSingletonComposer(
                       exportTypesArray,
                       conventions,
                       disposeCompositionHostsWithThis,
                       disposeProvidersWithThis,
                       handleNewExports));
        }