1 // -----------------------------------------------------------------------
\r
2 // Copyright (c) Microsoft Corporation. All rights reserved.
\r
3 // -----------------------------------------------------------------------
\r
5 using System.ComponentModel.Composition.Hosting;
\r
6 using System.ComponentModel.Composition.Primitives;
\r
7 using System.Globalization;
\r
9 using System.Reflection;
\r
11 namespace System.ComponentModel.Composition
\r
13 public static partial class CompositionInitializer
\r
16 /// Will satisfy the imports on a object instance based on a <see cref="CompositionContainer"/>
\r
17 /// registered with the <see cref="CompositionHost"/>. By default if no <see cref="CompositionContainer"/>
\r
18 /// is registered the first time this is called it will be initialized to a catalog
\r
19 /// that contains all the assemblies loaded by the initial application XAP.
\r
21 /// <param name="attributedPart">
\r
22 /// Object instance that contains <see cref="ImportAttribute"/>s that need to be satisfied.
\r
24 /// <exception cref="ArgumentNullException">
\r
25 /// <paramref name="instance"/> is <see langword="null"/>.
\r
27 /// <exception cref="ArgumentException">
\r
28 /// <paramref name="instance"/> contains <see cref="ExportAttribute"/>s applied on its type.
\r
30 /// <exception cref="ChangeRejectedException">
\r
31 /// One or more of the imports on the object instance could not be satisfied.
\r
33 /// <exception cref="CompositionException">
\r
34 /// One or more of the imports on the object instance caused an error while composing.
\r
36 public static void SatisfyImports(object attributedPart)
\r
38 if (attributedPart == null)
\r
40 throw new ArgumentNullException("attributedPart");
\r
42 ComposablePart part = AttributedModelServices.CreatePart(attributedPart);
\r
43 CompositionInitializer.SatisfyImports(part);
\r
48 /// Will satisfy the imports on a part based on a <see cref="CompositionContainer"/>
\r
49 /// registered with the <see cref="CompositionHost"/>. By default if no <see cref="CompositionContainer"/>
\r
50 /// is registered the first time this is called it will be initialized to a catalog
\r
51 /// that contains all the assemblies loaded by the initial application XAP.
\r
53 /// <param name="part">
\r
54 /// Part with imports that need to be satisfied.
\r
56 /// <exception cref="ArgumentNullException">
\r
57 /// <paramref name="instance"/> is <see langword="null"/>.
\r
59 /// <exception cref="ArgumentException">
\r
60 /// <paramref name="instance"/> contains <see cref="ExportAttribute"/>s applied on its type.
\r
62 /// <exception cref="ChangeRejectedException">
\r
63 /// One or more of the imports on the object instance could not be satisfied.
\r
65 /// <exception cref="CompositionException">
\r
66 /// One or more of the imports on the object instance caused an error while composing.
\r
68 public static void SatisfyImports(ComposablePart part)
\r
72 throw new ArgumentNullException("part");
\r
75 var batch = new CompositionBatch();
\r
77 batch.AddPart(part);
\r
79 if (part.ExportDefinitions.Any())
\r
81 throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,
\r
82 Strings.ArgumentException_TypeHasExports, part.ToString()), "part");
\r
85 CompositionContainer container = null;
\r
87 // Ignoring return value because we don't need to know if we created it or not
\r
88 CompositionHost.TryGetOrCreateContainer(_createContainer, out container);
\r
90 container.Compose(batch);
\r
93 private static Func<CompositionContainer> _createContainer = CreateCompositionContainer;
\r
94 private static CompositionContainer CreateCompositionContainer()
\r
96 var assemblyCatalogs = GetAssemblyList()
\r
97 .Select<Assembly, ComposablePartCatalog>(assembly => new AssemblyCatalog(assembly));
\r
99 var aggCatalog = new AggregateCatalog(assemblyCatalogs);
\r
101 return new CompositionContainer(aggCatalog);
\r