예제 #1
0
        /// <summary>
        ///     Will satisfy the imports on a object instance based on a global <see cref="CompositionContainer"/>.
        ///		By default if no <see cref="CompositionContainer"/> is registered the first time this is called it will be
        ///		initialized to a catalog that contains all the assemblies loaded by the initial application XAP or in the
        ///		current AppDomain.
        /// </summary>
        /// <param name="part">
        ///     Part with imports that need to be satisfied.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="part"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="ArgumentException">
        ///     <paramref name="part"/> contains <see cref="ExportAttribute"/>s applied on its type.
        /// </exception>
        /// <exception cref="ChangeRejectedException">
        ///     One or more of the imports on the object instance could not be satisfied.
        /// </exception>
        /// <exception cref="CompositionException">
        ///     One or more of the imports on the object instance caused an error while composing.
        /// </exception>
        public static void SatisfyImports(ComposablePart part)
        {
            if (part == null)
            {
                throw new ArgumentNullException("part");
            }

            CompositionBatch batch = new CompositionBatch();

            batch.AddPart(part);

            if (part.ExportDefinitions.Any())
            {
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "Type {0} has unsatisfied exports", part), "part");
            }

            GlobalContainer.Compose(batch);
        }