Example #1
0
        /// <summary>Fills the imports of the specified part.</summary>
        /// <param name="part">The part to fill the imports of.</param>
        /// <exception cref="T:System.ArgumentNullException">
        /// <paramref name="part" /> is null.</exception>
        /// <exception cref="T:System.ArgumentException">
        /// <paramref name="part" /> contains exports.</exception>
        /// <exception cref="T:System.ComponentModel.Composition.ChangeRejectedException">One or more of the imports of <paramref name="part" /> could not be satisfied.</exception>
        /// <exception cref="T:System.ComponentModel.Composition.CompositionException">One or more of the imports of <paramref name="part" /> caused a composition error.</exception>
        public static void SatisfyImports(ComposablePart part)
        {
            if (part == null)
            {
                throw new ArgumentNullException(nameof(part));
            }

            CompositionBatch batch = new CompositionBatch();

            batch.AddPart(part);

            if (part.ExportDefinitions.Any())
            {
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "Error type has export {0}", part.ToString()), nameof(part));
            }

            CompositionContainer globalContainer;

            // UGiacobbi 2000105 The original SL implementation allows us to pass a delegate and create a container, for now this is not support so me need a non-null container.

            // we need a non null container to prevent NullReferenceExceptions
            CompositionHost.TryGetOrCreateContainer(() => _container, out globalContainer);

            globalContainer.Compose(batch);
        }
Example #2
0
        /// <summary>
        ///     Will satisfy the imports on a part based on a <see cref="CompositionContainer"/>
        ///     registered with the <see cref="CompositionHost"/>. 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.
        /// </summary>
        /// <param name="part">
        ///     Part with imports that need to be satisfied.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="instance"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="ArgumentException">
        ///     <paramref name="instance"/> 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");
            }

            var batch = new CompositionBatch();

            batch.AddPart(part);

            if (part.ExportDefinitions.Any())
            {
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,
                                                          Strings.ArgumentException_TypeHasExports, part.ToString()), "part");
            }

            CompositionContainer container = null;

            // Ignoring return value because we don't need to know if we created it or not
            CompositionHost.TryGetOrCreateContainer(_createContainer, out container);

            container.Compose(batch);
        }
Example #3
0
        /// <summary>
        ///     Will satisfy the imports on a object instance based on a <see cref="CompositionContainer"/>
        ///     registered with the <see cref="CompositionHost"/>. 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.
        /// </summary>
        /// <param name="instance">
        ///     Object instance that contains <see cref="ImportAttribute"/>s that need to be satisfied.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="instance"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="ArgumentException">
        ///     <paramref name="instance"/> 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>
        #endregion // Documentation
        public static void SatisfyImports(object instance)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }

            //var batch = new CompositionBatch();

            //var attributedPart = batch.AddPart(instance);

            //if (attributedPart.ExportDefinitions.Any())
            //{
            //    throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,
            //            Strings.ArgumentException_TypeHasExports, instance.GetType().FullName), "instance");
            //}

            CompositionContainer container = null;

            // Ignoring return value because we don't need to know if we created it or not
            CompositionHost.TryGetOrCreateContainer(_createContainer, out container);

            container.ComposeParts(instance);
        }