2010-06-21 Atsushi Enomoto <atsushi@ximian.com>
[mcs.git] / class / System.ComponentModel.Composition / src / ComponentModel / System / ComponentModel / Composition / AttributedModelServices.cs
blobb2be75773a22cbf2bc733cd15fa30848a4d23301
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.Runtime.Serialization;
8 using System.ComponentModel.Composition.AttributedModel;
9 using System.Reflection;
10 using System.Linq;
11 using Microsoft.Internal;
12 using System.Collections.Generic;
13 using System.Diagnostics.CodeAnalysis;
15 namespace System.ComponentModel.Composition
17 public static class AttributedModelServices
19 [SuppressMessage("Microsoft.Design", "CA1004")]
20 public static TMetadataView GetMetadataView<TMetadataView>(IDictionary<string, object> metadata)
22 Requires.NotNull(metadata, "metadata");
24 return MetadataViewProvider.GetMetadataView<TMetadataView>(metadata);
27 public static ComposablePart CreatePart(object attributedPart)
29 Requires.NotNull(attributedPart, "attributedPart");
30 return AttributedModelDiscovery.CreatePart(attributedPart);
33 public static ComposablePartDefinition CreatePartDefinition(Type type, ICompositionElement origin)
35 Requires.NotNull(type, "type");
36 return AttributedModelServices.CreatePartDefinition(type, origin, false);
39 public static ComposablePartDefinition CreatePartDefinition(Type type, ICompositionElement origin, bool ensureIsDiscoverable)
41 Requires.NotNull(type, "type");
42 if (ensureIsDiscoverable)
44 return AttributedModelDiscovery.CreatePartDefinitionIfDiscoverable(type, origin);
46 else
48 return AttributedModelDiscovery.CreatePartDefinition(type, null, false, origin);
52 public static string GetTypeIdentity(Type type)
54 Requires.NotNull(type, "type");
56 return ContractNameServices.GetTypeIdentity(type);
59 public static string GetTypeIdentity(MethodInfo method)
61 Requires.NotNull(method, "method");
63 return ContractNameServices.GetTypeIdentityFromMethod(method);
66 public static string GetContractName(Type type)
68 return AttributedModelServices.GetTypeIdentity(type);
71 public static ComposablePart AddExportedValue<T>(this CompositionBatch batch, T exportedValue)
73 Requires.NotNull(batch, "batch");
74 string contractName = AttributedModelServices.GetContractName(typeof(T));
76 return batch.AddExportedValue<T>(contractName, exportedValue);
79 public static void ComposeExportedValue<T>(this CompositionContainer container, T exportedValue)
81 Requires.NotNull(container, "container");
83 CompositionBatch batch = new CompositionBatch();
84 batch.AddExportedValue<T>(exportedValue);
85 container.Compose(batch);
88 public static ComposablePart AddExportedValue<T>(this CompositionBatch batch, string contractName, T exportedValue)
90 Requires.NotNull(batch, "batch");
92 string typeIdentity = AttributedModelServices.GetTypeIdentity(typeof(T));
94 IDictionary<string, object> metadata = new Dictionary<string, object>();
95 metadata.Add(CompositionConstants.ExportTypeIdentityMetadataName, typeIdentity);
97 return batch.AddExport(new Export(contractName, metadata, () => exportedValue));
100 public static void ComposeExportedValue<T>(this CompositionContainer container, string contractName, T exportedValue)
102 Requires.NotNull(container, "container");
104 CompositionBatch batch = new CompositionBatch();
105 batch.AddExportedValue<T>(contractName, exportedValue);
106 container.Compose(batch);
109 public static ComposablePart AddPart(this CompositionBatch batch, object attributedPart)
111 Requires.NotNull(batch, "batch");
112 Requires.NotNull(attributedPart, "attributedPart");
114 ComposablePart part = AttributedModelServices.CreatePart(attributedPart);
116 batch.AddPart(part);
118 return part;
121 public static void ComposeParts(this CompositionContainer container, params object[] attributedParts)
123 Requires.NotNull(container, "container");
124 Requires.NotNullOrNullElements(attributedParts, "attributedParts");
126 CompositionBatch batch = new CompositionBatch(
127 attributedParts.Select(attributedPart => AttributedModelServices.CreatePart(attributedPart)).ToArray(),
128 Enumerable.Empty<ComposablePart>());
130 container.Compose(batch);
133 /// <summary>
134 /// Satisfies the imports of the specified attributed object exactly once and they will not
135 /// ever be recomposed.
136 /// </summary>
137 /// <param name="part">
138 /// The attributed object to set the imports.
139 /// </param>
140 /// <exception cref="ArgumentNullException">
141 /// <paramref name="attributedPart"/> is <see langword="null"/>.
142 /// </exception>
143 /// <exception cref="CompositionException">
144 /// An error occurred during composition. <see cref="CompositionException.Errors"/> will
145 /// contain a collection of errors that occurred.
146 /// </exception>
147 /// <exception cref="ObjectDisposedException">
148 /// The <see cref="ICompositionService"/> has been disposed of.
149 /// </exception>
150 public static ComposablePart SatisfyImportsOnce(this ICompositionService compositionService, object attributedPart)
152 Requires.NotNull(compositionService, "compositionService");
153 Requires.NotNull(attributedPart, "attributedPart");
155 ComposablePart part = AttributedModelServices.CreatePart(attributedPart);
156 compositionService.SatisfyImportsOnce(part);
158 return part;