Fix bugs in sizing TableLayoutPanel (Xamarin bug 18638)
[mono.git] / mcs / class / System.ComponentModel.Composition / src / Composition.Initialization / System / ComponentModel / Composition / CompositionInitializer.cs
1 // -----------------------------------------------------------------------\r
2 // Copyright (c) Microsoft Corporation.  All rights reserved.\r
3 // -----------------------------------------------------------------------\r
4 using System;\r
5 using System.ComponentModel.Composition.Hosting;\r
6 using System.ComponentModel.Composition.Primitives;\r
7 using System.Globalization;\r
8 using System.Linq;\r
9 using System.Reflection;\r
10 \r
11 namespace System.ComponentModel.Composition\r
12 {\r
13     public static partial class CompositionInitializer\r
14     {\r
15         /// <summary>\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
20         /// </summary>\r
21         /// <param name="attributedPart">\r
22         ///     Object instance that contains <see cref="ImportAttribute"/>s that need to be satisfied.\r
23         /// </param>\r
24         /// <exception cref="ArgumentNullException">\r
25         ///     <paramref name="instance"/> is <see langword="null"/>.\r
26         /// </exception>\r
27         /// <exception cref="ArgumentException">\r
28         ///     <paramref name="instance"/> contains <see cref="ExportAttribute"/>s applied on its type.\r
29         /// </exception>\r
30         /// <exception cref="ChangeRejectedException">\r
31         ///     One or more of the imports on the object instance could not be satisfied.\r
32         /// </exception>\r
33         /// <exception cref="CompositionException">\r
34         ///     One or more of the imports on the object instance caused an error while composing.\r
35         /// </exception>\r
36         public static void SatisfyImports(object attributedPart)\r
37         {\r
38             if (attributedPart == null)\r
39             {\r
40                 throw new ArgumentNullException("attributedPart");\r
41             }\r
42             ComposablePart part = AttributedModelServices.CreatePart(attributedPart);\r
43             CompositionInitializer.SatisfyImports(part);\r
44         }\r
45 \r
46 \r
47         /// <summary>\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
52         /// </summary>\r
53         /// <param name="part">\r
54         ///     Part with imports that need to be satisfied.\r
55         /// </param>\r
56         /// <exception cref="ArgumentNullException">\r
57         ///     <paramref name="instance"/> is <see langword="null"/>.\r
58         /// </exception>\r
59         /// <exception cref="ArgumentException">\r
60         ///     <paramref name="instance"/> contains <see cref="ExportAttribute"/>s applied on its type.\r
61         /// </exception>\r
62         /// <exception cref="ChangeRejectedException">\r
63         ///     One or more of the imports on the object instance could not be satisfied.\r
64         /// </exception>\r
65         /// <exception cref="CompositionException">\r
66         ///     One or more of the imports on the object instance caused an error while composing.\r
67         /// </exception>\r
68         public static void SatisfyImports(ComposablePart part)\r
69         {\r
70             if (part == null)\r
71             {\r
72                 throw new ArgumentNullException("part");\r
73             }\r
74 \r
75             var batch = new CompositionBatch();\r
76 \r
77             batch.AddPart(part);\r
78 \r
79             if (part.ExportDefinitions.Any())\r
80             {\r
81                 throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,\r
82                         Strings.ArgumentException_TypeHasExports, part.ToString()), "part");\r
83             }\r
84 \r
85             CompositionContainer container = null;\r
86 \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
89 \r
90             container.Compose(batch);\r
91         }\r
92 \r
93         private static Func<CompositionContainer> _createContainer = CreateCompositionContainer;\r
94         private static CompositionContainer CreateCompositionContainer()\r
95         {\r
96             var assemblyCatalogs = GetAssemblyList()\r
97                 .Select<Assembly, ComposablePartCatalog>(assembly => new AssemblyCatalog(assembly));\r
98 \r
99             var aggCatalog = new AggregateCatalog(assemblyCatalogs);\r
100 \r
101             return new CompositionContainer(aggCatalog);\r
102         }\r
103     }\r
104 }