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
.Runtime
.Serialization
;
8 using System
.ComponentModel
.Composition
.AttributedModel
;
9 using System
.Reflection
;
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
);
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
);
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
);
134 /// Satisfies the imports of the specified attributed object exactly once and they will not
135 /// ever be recomposed.
137 /// <param name="part">
138 /// The attributed object to set the imports.
140 /// <exception cref="ArgumentNullException">
141 /// <paramref name="attributedPart"/> is <see langword="null"/>.
143 /// <exception cref="CompositionException">
144 /// An error occurred during composition. <see cref="CompositionException.Errors"/> will
145 /// contain a collection of errors that occurred.
147 /// <exception cref="ObjectDisposedException">
148 /// The <see cref="ICompositionService"/> has been disposed of.
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
);