1
// -----------------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 // -----------------------------------------------------------------------
5 using System
.Collections
.Generic
;
6 using System
.ComponentModel
.Composition
.Primitives
;
7 using System
.Diagnostics
.CodeAnalysis
;
8 using System
.Globalization
;
10 using System
.Linq
.Expressions
;
11 using System
.Collections
.ObjectModel
;
12 using Microsoft
.Internal
;
13 using Microsoft
.Internal
.Collections
;
15 namespace System
.ComponentModel
.Composition
.Hosting
17 public abstract partial class ExportProvider
20 /// Returns the export with the contract name derived from the specified type parameter,
21 /// throwing an exception if there is not exactly one matching export.
23 /// <typeparam name="T">
24 /// The type of the <see cref="Lazy{T}"/> object to return. The contract name is also
25 /// derived from this type parameter.
28 /// The <see cref="Lazy{T}"/> object with the contract name derived from
29 /// <typeparamref name="T"/>.
33 /// The returned <see cref="Lazy{T}"/> object is an instance of
34 /// <see cref="Lazy{T, TMetadataView}"/> underneath, where
35 /// <c>TMetadataView</c>
36 /// is <see cref="IDictionary{TKey, TValue}"/> and where <c>TKey</c>
37 /// is <see cref="String"/> and <c>TValue</c> is <see cref="Object"/>.
40 /// The contract name is the result of calling
41 /// <see cref="AttributedModelServices.GetContractName(Type)"/> on <typeparamref name="T"/>.
44 /// The contract name is compared using a case-sensitive, non-linguistic comparison
45 /// using <see cref="StringComparer.Ordinal"/>.
48 /// <exception cref="ImportCardinalityMismatchException">
50 /// There are zero <see cref="Lazy{T}"/> objects with the contract name derived
51 /// from <typeparamref name="T"/> in the <see cref="CompositionContainer"/>.
55 /// There are more than one <see cref="Lazy{T}"/> objects with the contract name
56 /// derived from <typeparamref name="T"/> in the <see cref="CompositionContainer"/>.
59 /// <exception cref="ObjectDisposedException">
60 /// The <see cref="CompositionContainer"/> has been disposed of.
62 public Lazy
<T
> GetExport
<T
>()
64 return this.GetExport
<T
>((string)null);
68 /// Returns the export with the specified contract name, throwing an exception if there
69 /// is not exactly one matching export.
71 /// <typeparam name="T">
72 /// The type of the <see cref="Lazy{T}"/> object to return.
74 /// <param name="contractName">
75 /// A <see cref="String"/> containing the contract name of the <see cref="Lazy{T}"/>
76 /// object to return; or <see langword="null"/> or an empty string ("") to use the
77 /// default contract name.
80 /// The <see cref="Lazy{T}"/> object with the specified contract name.
84 /// The returned <see cref="Lazy{T}"/> object is an instance of
85 /// <see cref="Lazy{T, TMetadataView}"/> underneath, where
86 /// <c>TMetadataView</c>
87 /// is <see cref="IDictionary{TKey, TValue}"/> and where <c>TKey</c>
88 /// is <see cref="String"/> and <c>TValue</c> is <see cref="Object"/>.
91 /// The contract name is the result of calling
92 /// <see cref="AttributedModelServices.GetContractName(Type)"/> on <typeparamref name="T"/>.
95 /// The default contract name is compared using a case-sensitive, non-linguistic
96 /// comparison using <see cref="StringComparer.Ordinal"/>.
99 /// <exception cref="ImportCardinalityMismatchException">
101 /// There are zero <see cref="Lazy{T}"/> objects with the specified contract name
102 /// in the <see cref="CompositionContainer"/>.
106 /// There are more than one <see cref="Lazy{T}"/> objects with the specified contract
107 /// name in the <see cref="CompositionContainer"/>.
110 /// <exception cref="ObjectDisposedException">
111 /// The <see cref="CompositionContainer"/> has been disposed of.
113 public Lazy
<T
> GetExport
<T
>(string contractName
)
115 return this.GetExportCore
<T
>(contractName
);
119 /// Returns the export with the contract name derived from the specified type parameter,
120 /// throwing an exception if there is not exactly one matching export.
122 /// <typeparam name="T">
123 /// The type of the <see cref="Lazy{T, TMetadataView}"/> object to return. The
124 /// contract name is also derived from this type parameter.
126 /// <typeparam name="TMetadataView">
127 /// The type of the metadata view of the <see cref="Lazy{T, TMetadataView}"/> object
131 /// The <see cref="Lazy{T, TMetadataView}"/> object with the contract name derived
132 /// from <typeparamref name="T"/>.
136 /// The contract name is the result of calling
137 /// <see cref="AttributedModelServices.GetContractName(Type)"/> on <typeparamref name="T"/>.
140 /// The contract name is compared using a case-sensitive, non-linguistic comparison
141 /// using <see cref="StringComparer.Ordinal"/>.
144 /// <exception cref="ImportCardinalityMismatchException">
146 /// There are zero <see cref="Lazy{T, TMetadataView}"/> objects with the contract
147 /// name derived from <typeparamref name="T"/> in the
148 /// <see cref="CompositionContainer"/>.
152 /// There are more than one <see cref="Lazy{T, TMetadataView}"/> objects with the
153 /// contract name derived from <typeparamref name="T"/> in the
154 /// <see cref="CompositionContainer"/>.
157 /// <exception cref="InvalidOperationException">
158 /// <typeparamref name="TMetadataView"/> is not a valid metadata view type.
160 /// <exception cref="ObjectDisposedException">
161 /// The <see cref="CompositionContainer"/> has been disposed of.
163 public Lazy
<T
, TMetadataView
> GetExport
<T
, TMetadataView
>()
165 return this.GetExport
<T
, TMetadataView
>((string)null);
169 /// Returns the export with the specified contract name, throwing an exception if there
170 /// is not exactly one matching export.
172 /// <typeparam name="T">
173 /// The type of the <see cref="Lazy{T, TMetadataView}"/> object to return.
175 /// <typeparam name="TMetadataView">
176 /// The type of the metadata view of the <see cref="Lazy{T, TMetadataView}"/> object
179 /// <param name="contractName">
180 /// A <see cref="String"/> containing the contract name of the
181 /// <see cref="Lazy{T, TMetadataView}"/> object to return; or <see langword="null"/>
182 /// or an empty string ("") to use the default contract name.
185 /// The <see cref="Lazy{T, TMetadataView}"/> object with the specified contract name.
189 /// The default contract name is the result of calling
190 /// <see cref="AttributedModelServices.GetContractName(Type)"/> on <typeparamref name="T"/>.
193 /// The contract name is compared using a case-sensitive, non-linguistic comparison
194 /// using <see cref="StringComparer.Ordinal"/>.
197 /// <exception cref="ImportCardinalityMismatchException">
199 /// There are zero <see cref="Lazy{T, TMetadataView}"/> objects with the
200 /// specified contract name in the <see cref="CompositionContainer"/>.
204 /// There are more than one <see cref="Lazy{T, TMetadataView}"/> objects with the
205 /// specified contract name in the <see cref="CompositionContainer"/>.
208 /// <exception cref="InvalidOperationException">
209 /// <typeparamref name="TMetadataView"/> is not a valid metadata view type.
211 /// <exception cref="ObjectDisposedException">
212 /// The <see cref="CompositionContainer"/> has been disposed of.
214 public Lazy
<T
, TMetadataView
> GetExport
<T
, TMetadataView
>(string contractName
)
216 return this.GetExportCore
<T
, TMetadataView
>(contractName
);
220 /// Returns the exports with the specified contract name.
222 /// <param name="type">
223 /// The <see cref="Type"/> of the <see cref="Export"/> objects to return.
225 /// <param name="metadataViewType">
226 /// The <see cref="Type"/> of the metadata view of the <see cref="Export"/> objects to
229 /// <param name="contractName">
230 /// A <see cref="String"/> containing the contract name of the
231 /// <see cref="Export"/> object to return; or <see langword="null"/>
232 /// or an empty string ("") to use the default contract name.
235 /// An <see cref="IEnumerable{T}"/> containing the <see cref="Lazy{Object, Object}"/> objects
236 /// with the specified contract name, if found; otherwise, an empty
237 /// <see cref="IEnumerable{T}"/>.
241 /// The returned <see cref="Export"/> objects are instances of
242 /// <see cref="Lazy{T, TMetadataView}"/> underneath, where <c>T</c>
243 /// is <paramref name="type"/> and <c>TMetadataView</c> is
244 /// <paramref name="metadataViewType"/>.
247 /// The default contract name is the result of calling
248 /// <see cref="AttributedModelServices.GetContractName(Type)"/> on <paramref name="type"/>.
251 /// The contract name is compared using a case-sensitive, non-linguistic comparison
252 /// using <see cref="StringComparer.Ordinal"/>.
255 /// <exception cref="ArgumentNullException">
256 /// <paramref name="type"/> is <see langword="null"/>.
258 /// <exception cref="InvalidOperationException">
259 /// <paramref name="metadataViewType"/> is not a valid metadata view type.
261 /// <exception cref="ObjectDisposedException">
262 /// The <see cref="CompositionContainer"/> has been disposed of.
264 [SuppressMessage("Microsoft.Design", "CA1006")]
265 public IEnumerable
<Lazy
<object, object>> GetExports(Type type
, Type metadataViewType
, string contractName
)
267 IEnumerable
<Export
> exports
= this.GetExportsCore(type
, metadataViewType
, contractName
, ImportCardinality
.ZeroOrMore
);
268 Collection
<Lazy
<object, object>> result
= new Collection
<Lazy
<object, object>>();
270 Func
<Export
, Lazy
<object, object>> typedExportFactory
= ExportServices
.CreateSemiStronglyTypedLazyFactory(type
, metadataViewType
);
271 foreach (Export export
in exports
)
273 result
.Add(typedExportFactory
.Invoke(export
));
280 /// Returns the exports with the contract name derived from the specified type parameter.
282 /// <typeparam name="T">
283 /// The type of the <see cref="Lazy{T}"/> objects to return. The contract name is also
284 /// derived from this type parameter.
287 /// An <see cref="IEnumerable{T}"/> containing the <see cref="Lazy{T}"/> objects
288 /// with the contract name derived from <typeparamref name="T"/>, if found; otherwise,
289 /// an empty <see cref="IEnumerable{T}"/>.
293 /// The returned <see cref="Lazy{T}"/> objects are instances of
294 /// <see cref="Lazy{T, TMetadataView}"/> underneath, where
295 /// <c>TMetadataView</c>
296 /// is <see cref="IDictionary{TKey, TValue}"/> and where <c>TKey</c>
297 /// is <see cref="String"/> and <c>TValue</c> is <see cref="Object"/>.
300 /// The contract name is the result of calling
301 /// <see cref="AttributedModelServices.GetContractName(Type)"/> on <typeparamref name="T"/>.
304 /// The contract name is compared using a case-sensitive, non-linguistic comparison
305 /// using <see cref="StringComparer.Ordinal"/>.
308 /// <exception cref="ObjectDisposedException">
309 /// The <see cref="CompositionContainer"/> has been disposed of.
311 [SuppressMessage("Microsoft.Design", "CA1006")]
312 public IEnumerable
<Lazy
<T
>> GetExports
<T
>()
314 return this.GetExports
<T
>((string)null);
318 /// Returns the exports with the specified contract name.
320 /// <typeparam name="T">
321 /// The type of the <see cref="Lazy{T}"/> objects to return.
323 /// <param name="contractName">
324 /// A <see cref="String"/> containing the contract name of the <see cref="Lazy{T}"/>
325 /// objects to return; or <see langword="null"/> or an empty string ("") to use the
326 /// default contract name.
329 /// An <see cref="IEnumerable{T}"/> containing the <see cref="Lazy{T}"/> objects
330 /// with the specified contract name, if found; otherwise, an empty
331 /// <see cref="IEnumerable{T}"/>.
335 /// The returned <see cref="Lazy{T}"/> objects are instances of
336 /// <see cref="Lazy{T, TMetadataView}"/> underneath, where
337 /// <c>TMetadataView</c>
338 /// is <see cref="IDictionary{TKey, TValue}"/> and where <c>TKey</c>
339 /// is <see cref="String"/> and <c>TValue</c> is <see cref="Object"/>.
342 /// The default contract name is the result of calling
343 /// <see cref="AttributedModelServices.GetContractName(Type)"/> on <typeparamref name="T"/>.
346 /// The contract name is compared using a case-sensitive, non-linguistic comparison
347 /// using <see cref="StringComparer.Ordinal"/>.
350 /// <exception cref="ObjectDisposedException">
351 /// The <see cref="CompositionContainer"/> has been disposed of.
353 [SuppressMessage("Microsoft.Design", "CA1006")]
354 public IEnumerable
<Lazy
<T
>> GetExports
<T
>(string contractName
)
356 return this.GetExportsCore
<T
>(contractName
);
360 /// Returns the exports with the contract name derived from the specified type parameter.
362 /// <typeparam name="T">
363 /// The type of the <see cref="Lazy{T, TMetadataView}"/> objects to return. The
364 /// contract name is also derived from this type parameter.
366 /// <typeparam name="TMetadataView">
367 /// The type of the metadata view of the <see cref="Lazy{T, TMetadataView}"/> objects
371 /// An <see cref="IEnumerable{T}"/> containing the
372 /// <see cref="Lazy{T, TMetadataView}"/> objects with the contract name derived from
373 /// <typeparamref name="T"/>, if found; otherwise, an empty
374 /// <see cref="IEnumerable{T}"/>.
378 /// The contract name is the result of calling
379 /// <see cref="AttributedModelServices.GetContractName(Type)"/> on <typeparamref name="T"/>.
382 /// The contract name is compared using a case-sensitive, non-linguistic comparison
383 /// using <see cref="StringComparer.Ordinal"/>.
386 /// <exception cref="InvalidOperationException">
387 /// <typeparamref name="TMetadataView"/> is not a valid metadata view type.
389 /// <exception cref="ObjectDisposedException">
390 /// The <see cref="CompositionContainer"/> has been disposed of.
392 [SuppressMessage("Microsoft.Design", "CA1006")]
393 public IEnumerable
<Lazy
<T
, TMetadataView
>> GetExports
<T
, TMetadataView
>()
395 return this.GetExports
<T
, TMetadataView
>((string)null);
399 /// Returns the exports with the specified contract name.
401 /// <typeparam name="T">
402 /// The type of the <see cref="Lazy{T, TMetadataView}"/> objects to return. The
403 /// contract name is also derived from this type parameter.
405 /// <typeparam name="TMetadataView">
406 /// The type of the metadata view of the <see cref="Lazy{T, TMetadataView}"/> objects
409 /// <param name="contractName">
410 /// A <see cref="String"/> containing the contract name of the
411 /// <see cref="Lazy{T, TMetadataView}"/> objects to return; or <see langword="null"/>
412 /// or an empty string ("") to use the default contract name.
415 /// An <see cref="IEnumerable{T}"/> containing the
416 /// <see cref="Lazy{T, TMetadataView}"/> objects with the specified contract name if
417 /// found; otherwise, an empty <see cref="IEnumerable{T}"/>.
421 /// The default contract name is the result of calling
422 /// <see cref="AttributedModelServices.GetContractName(Type)"/> on <typeparamref name="T"/>.
425 /// The contract name is compared using a case-sensitive, non-linguistic comparison
426 /// using <see cref="StringComparer.Ordinal"/>.
429 /// <exception cref="InvalidOperationException">
430 /// <typeparamref name="TMetadataView"/> is not a valid metadata view type.
432 /// <exception cref="ObjectDisposedException">
433 /// The <see cref="CompositionContainer"/> has been disposed of.
435 [SuppressMessage("Microsoft.Design", "CA1006")]
436 public IEnumerable
<Lazy
<T
, TMetadataView
>> GetExports
<T
, TMetadataView
>(string contractName
)
438 return this.GetExportsCore
<T
, TMetadataView
>(contractName
);
442 /// Returns the exported value with the contract name derived from the specified type
443 /// parameter, throwing an exception if there is not exactly one matching exported value.
445 /// <typeparam name="T">
446 /// The type of the exported value to return. The contract name is also
447 /// derived from this type parameter.
450 /// The exported <see cref="Object"/> with the contract name derived from
451 /// <typeparamref name="T"/>.
455 /// The contract name is the result of calling
456 /// <see cref="AttributedModelServices.GetContractName(Type)"/> on <typeparamref name="T"/>.
459 /// The contract name is compared using a case-sensitive, non-linguistic comparison
460 /// using <see cref="StringComparer.Ordinal"/>.
463 /// <exception cref="CompositionContractMismatchException">
464 /// The underlying exported value cannot be cast to <typeparamref name="T"/>.
466 /// <exception cref="ImportCardinalityMismatchException">
468 /// There are zero exported values with the contract name derived from
469 /// <typeparamref name="T"/> in the <see cref="CompositionContainer"/>.
473 /// There are more than one exported values with the contract name derived from
474 /// <typeparamref name="T"/> in the <see cref="CompositionContainer"/>.
477 /// <exception cref="ObjectDisposedException">
478 /// The <see cref="CompositionContainer"/> has been disposed of.
480 /// <exception cref="CompositionException">
481 /// An error occurred during composition. <see cref="CompositionException.Errors"/> will
482 /// contain a collection of errors that occurred.
484 public T GetExportedValue
<T
>()
486 return this.GetExportedValue
<T
>((string)null);
490 /// Returns the exported value with the specified contract name, throwing an exception
491 /// if there is not exactly one matching exported value.
493 /// <typeparam name="T">
494 /// The type of the exported value to return.
496 /// <param name="contractName">
497 /// A <see cref="String"/> containing the contract name of the exported value to return,
498 /// or <see langword="null"/> or an empty string ("") to use the default contract name.
501 /// The exported <see cref="Object"/> with the specified contract name.
505 /// The default contract name is the result of calling
506 /// <see cref="AttributedModelServices.GetContractName(Type)"/> on <typeparamref name="T"/>.
509 /// The contract name is compared using a case-sensitive, non-linguistic comparison
510 /// using <see cref="StringComparer.Ordinal"/>.
513 /// <exception cref="CompositionContractMismatchException">
514 /// The underlying exported value cannot be cast to <typeparamref name="T"/>.
516 /// <exception cref="ImportCardinalityMismatchException">
518 /// There are zero exported values with the specified contract name in the
519 /// <see cref="CompositionContainer"/>.
523 /// There are more than one exported values with the specified contract name in the
524 /// <see cref="CompositionContainer"/>.
527 /// <exception cref="ObjectDisposedException">
528 /// The <see cref="CompositionContainer"/> has been disposed of.
530 /// <exception cref="CompositionException">
531 /// An error occurred during composition. <see cref="CompositionException.Errors"/> will
532 /// contain a collection of errors that occurred.
534 public T GetExportedValue
<T
>(string contractName
)
536 return this.GetExportedValueCore
<T
>(contractName
, ImportCardinality
.ExactlyOne
);
540 /// Returns the exported value with the contract name derived from the specified type
541 /// parameter, throwing an exception if there is more than one matching exported value.
543 /// <typeparam name="T">
544 /// The type of the exported value to return. The contract name is also
545 /// derived from this type parameter.
548 /// The exported <see cref="Object"/> with the contract name derived from
549 /// <typeparamref name="T"/>, if found; otherwise, the default value for
550 /// <typeparamref name="T"/>.
554 /// If the exported value is not found, then this method returns the appropriate
555 /// default value for <typeparamref name="T"/>; for example, 0 (zero) for integer
556 /// types, <see langword="false"/> for Boolean types, and <see langword="null"/>
557 /// for reference types.
560 /// The contract name is the result of calling
561 /// <see cref="AttributedModelServices.GetContractName(Type)"/> on <typeparamref name="T"/>.
564 /// The contract name is compared using a case-sensitive, non-linguistic comparison
565 /// using <see cref="StringComparer.Ordinal"/>.
568 /// <exception cref="CompositionContractMismatchException">
569 /// The underlying exported value cannot be cast to <typeparamref name="T"/>.
571 /// <exception cref="ImportCardinalityMismatchException">
573 /// There are more than one exported values with the contract name derived from
574 /// <typeparamref name="T"/> in the <see cref="CompositionContainer"/>.
577 /// <exception cref="ObjectDisposedException">
578 /// The <see cref="CompositionContainer"/> has been disposed of.
580 /// <exception cref="CompositionException">
581 /// An error occurred during composition. <see cref="CompositionException.Errors"/> will
582 /// contain a collection of errors that occurred.
584 public T GetExportedValueOrDefault
<T
>()
586 return this.GetExportedValueOrDefault
<T
>((string)null);
590 /// Returns the exported value with the specified contract name, throwing an exception
591 /// if there is more than one matching exported value.
593 /// <typeparam name="T">
594 /// The type of the exported value to return.
596 /// <param name="contractName">
597 /// A <see cref="String"/> containing the contract name of the exported value to return,
598 /// or <see langword="null"/> or an empty string ("") to use the default contract name.
601 /// The exported <see cref="Object"/> with the specified contract name, if found;
602 /// otherwise, the default value for <typeparamref name="T"/>.
606 /// If the exported value is not found, then this method returns the appropriate
607 /// default value for <typeparamref name="T"/>; for example, 0 (zero) for integer
608 /// types, <see langword="false"/> for Boolean types, and <see langword="null"/>
609 /// for reference types.
612 /// The default contract name is the result of calling
613 /// <see cref="AttributedModelServices.GetContractName(Type)"/> on <typeparamref name="T"/>.
616 /// The contract name is compared using a case-sensitive, non-linguistic comparison
617 /// using <see cref="StringComparer.Ordinal"/>.
620 /// <exception cref="CompositionContractMismatchException">
621 /// The underlying exported value cannot be cast to <typeparamref name="T"/>.
623 /// <exception cref="ImportCardinalityMismatchException">
624 /// There are more than one exported values with the specified contract name in the
625 /// <see cref="CompositionContainer"/>.
627 /// <exception cref="ObjectDisposedException">
628 /// The <see cref="CompositionContainer"/> has been disposed of.
630 /// <exception cref="CompositionException">
631 /// An error occurred during composition. <see cref="CompositionException.Errors"/> will
632 /// contain a collection of errors that occurred.
634 public T GetExportedValueOrDefault
<T
>(string contractName
)
636 return this.GetExportedValueCore
<T
>(contractName
, ImportCardinality
.ZeroOrOne
);
640 /// Returns the exported values with the contract name derived from the specified type
643 /// <typeparam name="T">
644 /// The type of the exported value to return. The contract name is also
645 /// derived from this type parameter.
648 /// An <see cref="Collection{T}"/> containing the exported values with the contract name
649 /// derived from the specified type parameter, if found; otherwise, an empty
650 /// <see cref="Collection{T}"/>.
654 /// The contract name is the result of calling
655 /// <see cref="AttributedModelServices.GetContractName(Type)"/> on <typeparamref name="T"/>.
658 /// The contract name is compared using a case-sensitive, non-linguistic comparison
659 /// using <see cref="StringComparer.Ordinal"/>.
662 /// <exception cref="CompositionContractMismatchException">
663 /// One or more of the underlying exported values cannot be cast to
664 /// <typeparamref name="T"/>.
666 /// <exception cref="ObjectDisposedException">
667 /// The <see cref="CompositionContainer"/> has been disposed of.
669 /// <exception cref="CompositionException">
670 /// An error occurred during composition. <see cref="CompositionException.Errors"/> will
671 /// contain a collection of errors that occurred.
673 public IEnumerable
<T
> GetExportedValues
<T
>()
675 return this.GetExportedValues
<T
>((string)null);
679 /// Returns the exported values with the specified contract name.
681 /// <typeparam name="T">
682 /// The type of the exported value to return.
684 /// <param name="contractName">
685 /// A <see cref="String"/> containing the contract name of the exported values to
686 /// return; or <see langword="null"/> or an empty string ("") to use the default
690 /// An <see cref="Collection{T}"/> containing the exported values with the specified
691 /// contract name, if found; otherwise, an empty <see cref="Collection{T}"/>.
695 /// The default contract name is the result of calling
696 /// <see cref="AttributedModelServices.GetContractName(Type)"/> on <typeparamref name="T"/>.
699 /// The contract name is compared using a case-sensitive, non-linguistic comparison
700 /// using <see cref="StringComparer.Ordinal"/>.
703 /// <exception cref="CompositionContractMismatchException">
704 /// One or more of the underlying exported values cannot be cast to
705 /// <typeparamref name="T"/>.
707 /// <exception cref="ObjectDisposedException">
708 /// The <see cref="CompositionContainer"/> has been disposed of.
710 /// <exception cref="CompositionException">
711 /// An error occurred during composition. <see cref="CompositionException.Errors"/> will
712 /// contain a collection of errors that occurred.
714 public IEnumerable
<T
> GetExportedValues
<T
>(string contractName
)
716 return this.GetExportedValuesCore
<T
>(contractName
);
719 private IEnumerable
<T
> GetExportedValuesCore
<T
>(string contractName
)
721 IEnumerable
<Export
> exports
= this.GetExportsCore(typeof(T
), (Type
)null, contractName
, ImportCardinality
.ZeroOrMore
);
723 Collection
<T
> result
= new Collection
<T
>();
724 foreach (Export export
in exports
)
726 result
.Add(ExportServices
.GetCastedExportedValue
<T
>(export
));
731 private T GetExportedValueCore
<T
>(string contractName
, ImportCardinality cardinality
)
733 Assumes
.IsTrue(cardinality
.IsAtMostOne());
735 Export export
= this.GetExportsCore(typeof(T
), (Type
)null, contractName
, cardinality
).SingleOrDefault();
737 return (export
!= null) ? ExportServices
.GetCastedExportedValue
<T
>(export
) : default(T
);
740 private IEnumerable
<Lazy
<T
>> GetExportsCore
<T
>(string contractName
)
742 IEnumerable
<Export
> exports
= this.GetExportsCore(typeof(T
), (Type
)null, contractName
, ImportCardinality
.ZeroOrMore
);
744 Collection
<Lazy
<T
>> result
= new Collection
<Lazy
<T
>>();
745 foreach (Export export
in exports
)
747 result
.Add(ExportServices
.CreateStronglyTypedLazyOfT
<T
>(export
));
752 private IEnumerable
<Lazy
<T
, TMetadataView
>> GetExportsCore
<T
, TMetadataView
>(string contractName
)
754 IEnumerable
<Export
> exports
= this.GetExportsCore(typeof(T
), typeof(TMetadataView
), contractName
, ImportCardinality
.ZeroOrMore
);
756 Collection
<Lazy
<T
, TMetadataView
>> result
= new Collection
<Lazy
<T
, TMetadataView
>>();
757 foreach (Export export
in exports
)
759 result
.Add(ExportServices
.CreateStronglyTypedLazyOfTM
<T
, TMetadataView
>(export
));
764 private Lazy
<T
, TMetadataView
> GetExportCore
<T
, TMetadataView
>(string contractName
)
766 Export export
= this.GetExportsCore(typeof(T
), typeof(TMetadataView
), contractName
, ImportCardinality
.ExactlyOne
).SingleOrDefault();
768 return (export
!= null) ? ExportServices
.CreateStronglyTypedLazyOfTM
<T
, TMetadataView
>(export
) : null;
771 private Lazy
<T
> GetExportCore
<T
>(string contractName
)
773 Export export
= this.GetExportsCore(typeof(T
), null, contractName
, ImportCardinality
.ExactlyOne
).SingleOrDefault();
775 return (export
!= null) ? ExportServices
.CreateStronglyTypedLazyOfT
<T
>(export
) : null;
778 private IEnumerable
<Export
> GetExportsCore(Type type
, Type metadataViewType
, string contractName
, ImportCardinality cardinality
)
780 // Only 'type' cannot be null - the other parameters have sensible defaults.
781 Requires
.NotNull(type
, "type");
783 if (string.IsNullOrEmpty(contractName
))
785 contractName
= AttributedModelServices
.GetContractName(type
);
788 if (metadataViewType
== null)
790 metadataViewType
= ExportServices
.DefaultMetadataViewType
;
793 if (!MetadataViewProvider
.IsViewTypeValid(metadataViewType
))
795 throw new InvalidOperationException(string.Format(CultureInfo
.CurrentCulture
, Strings
.InvalidMetadataView
, metadataViewType
.Name
));
798 ImportDefinition importDefinition
= BuildImportDefinition(type
, metadataViewType
, contractName
, cardinality
);
799 return this.GetExports(importDefinition
, null);
802 private static ImportDefinition
BuildImportDefinition(Type type
, Type metadataViewType
, string contractName
, ImportCardinality cardinality
)
804 Assumes
.NotNull(type
, metadataViewType
, contractName
);
806 IEnumerable
<KeyValuePair
<string, Type
>> requiredMetadata
= CompositionServices
.GetRequiredMetadata(metadataViewType
);
808 string requiredTypeIdentity
= null;
809 if (type
!= typeof(object))
811 requiredTypeIdentity
= AttributedModelServices
.GetTypeIdentity(type
);
814 return new ContractBasedImportDefinition(contractName
, requiredTypeIdentity
, requiredMetadata
, cardinality
, false, true, CreationPolicy
.Any
);