1 // -----------------------------------------------------------------------
\r
2 // Copyright (c) Microsoft Corporation. All rights reserved.
\r
3 // -----------------------------------------------------------------------
\r
5 using System.Collections.Generic;
\r
6 using System.ComponentModel.Composition.Hosting;
\r
7 using System.Diagnostics;
\r
8 using System.Diagnostics.CodeAnalysis;
\r
10 using System.Linq.Expressions;
\r
11 using Microsoft.Internal;
\r
13 namespace System.ComponentModel.Composition.Primitives
\r
16 /// Defines the <see langword="abstract"/> base class for composable part catalogs, which produce
\r
17 /// and return <see cref="ComposablePartDefinition"/> objects.
\r
20 /// This type is thread safe.
\r
22 [DebuggerTypeProxy(typeof(ComposablePartCatalogDebuggerProxy))]
\r
23 public abstract class ComposablePartCatalog : IDisposable
\r
25 private bool _isDisposed;
\r
28 /// Initializes a new instance of the <see cref="ComposablePartCatalog"/> class.
\r
30 protected ComposablePartCatalog()
\r
35 /// Gets the part definitions of the catalog.
\r
38 /// A <see cref="IQueryable{T}"/> of <see cref="ComposablePartDefinition"/> objects of the
\r
39 /// <see cref="ComposablePartCatalog"/>.
\r
41 /// <exception cref="ObjectDisposedException">
\r
42 /// The <see cref="ComposablePartCatalog"/> has been disposed of.
\r
45 /// <note type="inheritinfo">
\r
46 /// Overriders of this property should never return <see langword="null"/>.
\r
49 public abstract IQueryable<ComposablePartDefinition> Parts
\r
55 /// Returns the export definitions that match the constraint defined by the specified definition.
\r
57 /// <param name="definition">
\r
58 /// The <see cref="ImportDefinition"/> that defines the conditions of the
\r
59 /// <see cref="ExportDefinition"/> objects to return.
\r
62 /// An <see cref="IEnumerable{T}"/> of <see cref="Tuple{T1, T2}"/> containing the
\r
63 /// <see cref="ExportDefinition"/> objects and their associated
\r
64 /// <see cref="ComposablePartDefinition"/> for objects that match the constraint defined
\r
65 /// by <paramref name="definition"/>.
\r
67 /// <exception cref="ArgumentNullException">
\r
68 /// <paramref name="definition"/> is <see langword="null"/>.
\r
70 /// <exception cref="ObjectDisposedException">
\r
71 /// The <see cref="ComposablePartCatalog"/> has been disposed of.
\r
74 /// <note type="inheritinfo">
\r
75 /// Overriders of this property should never return <see langword="null"/>, if no
\r
76 /// <see cref="ExportDefinition"/> match the conditions defined by
\r
77 /// <paramref name="definition"/>, return an empty <see cref="IEnumerable{T}"/>.
\r
80 [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
\r
81 public virtual IEnumerable<Tuple<ComposablePartDefinition, ExportDefinition>> GetExports(ImportDefinition definition)
\r
83 this.ThrowIfDisposed();
\r
85 Requires.NotNull(definition, "definition");
\r
87 var exports = new List<Tuple<ComposablePartDefinition, ExportDefinition>>();
\r
88 foreach (var part in this.Parts)
\r
90 foreach (var export in part.ExportDefinitions)
\r
92 if (definition.IsConstraintSatisfiedBy(export))
\r
94 exports.Add(new Tuple<ComposablePartDefinition, ExportDefinition>(part, export));
\r
103 /// Releases the unmanaged resources used by the <see cref="ComposablePartCatalog"/> and
\r
104 /// optionally releases the managed resources.
\r
106 /// <param name="disposing">
\r
107 /// <see langword="true"/> to release both managed and unmanaged resources;
\r
108 /// <see langword="false"/> to release only unmanaged resources.
\r
110 public void Dispose()
\r
113 GC.SuppressFinalize(this);
\r
116 protected virtual void Dispose(bool disposing)
\r
118 this._isDisposed = true;
\r
121 private void ThrowIfDisposed()
\r
123 if (this._isDisposed)
\r
125 throw ExceptionBuilder.CreateObjectDisposed(this);
\r