1
// -----------------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 // -----------------------------------------------------------------------
5 using System
.ComponentModel
.Composition
.Hosting
;
6 using System
.ComponentModel
.Composition
.Primitives
;
7 using System
.Globalization
;
9 using System
.Reflection
;
11 namespace System
.ComponentModel
.Composition
13 public static partial class CompositionInitializer
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.
21 /// <param name="attributedPart">
22 /// Object instance that contains <see cref="ImportAttribute"/>s that need to be satisfied.
24 /// <exception cref="ArgumentNullException">
25 /// <paramref name="instance"/> is <see langword="null"/>.
27 /// <exception cref="ArgumentException">
28 /// <paramref name="instance"/> contains <see cref="ExportAttribute"/>s applied on its type.
30 /// <exception cref="ChangeRejectedException">
31 /// One or more of the imports on the object instance could not be satisfied.
33 /// <exception cref="CompositionException">
34 /// One or more of the imports on the object instance caused an error while composing.
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
);
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.
53 /// <param name="part">
54 /// Part with imports that need to be satisfied.
56 /// <exception cref="ArgumentNullException">
57 /// <paramref name="instance"/> is <see langword="null"/>.
59 /// <exception cref="ArgumentException">
60 /// <paramref name="instance"/> contains <see cref="ExportAttribute"/>s applied on its type.
62 /// <exception cref="ChangeRejectedException">
63 /// One or more of the imports on the object instance could not be satisfied.
65 /// <exception cref="CompositionException">
66 /// One or more of the imports on the object instance caused an error while composing.
68 public static void SatisfyImports(ComposablePart part
)
72 throw new ArgumentNullException("part");
75 var batch
= new CompositionBatch();
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
);