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