update MEF to preview 9
[mcs.git] / class / System.ComponentModel.Composition / src / ComponentModel / System / ComponentModel / Composition / Hosting / CompositionBatch.cs
blob62f0a4d4416eeaae23744ebd9d898e0842d6bc16
1 // -----------------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 // -----------------------------------------------------------------------
4 using System;
5 using System.Collections.Generic;
6 using System.ComponentModel.Composition.Primitives;
7 using System.Linq;
8 using Microsoft.Internal;
9 using System.Collections.ObjectModel;
10 using System.Diagnostics.CodeAnalysis;
12 namespace System.ComponentModel.Composition.Hosting
14 public partial class CompositionBatch
16 private object _lock = new object();
17 private bool _copyNeededForAdd;
18 private bool _copyNeededForRemove;
19 private List<ComposablePart> _partsToAdd;
20 private ReadOnlyCollection<ComposablePart> _readOnlyPartsToAdd;
21 private List<ComposablePart> _partsToRemove;
22 private ReadOnlyCollection<ComposablePart> _readOnlyPartsToRemove;
24 /// <summary>
25 /// Initializes a new instance of the <see cref="CompositionBatch"/> class.
26 /// </summary>
27 public CompositionBatch() :
28 this(null, null)
32 /// <summary>
33 /// Initializes a new instance of the <see cref="CompositionBatch"/> class.
34 /// </summary>
35 /// <param name="partsToAdd">The parts to add.</param>
36 /// <param name="partsToRemove">The parts to remove.</param>
37 public CompositionBatch(IEnumerable<ComposablePart> partsToAdd, IEnumerable<ComposablePart> partsToRemove)
39 this._partsToAdd = new List<ComposablePart>();
40 if (partsToAdd != null)
42 foreach (var part in partsToAdd)
44 if (part == null)
46 throw ExceptionBuilder.CreateContainsNullElement("partsToAdd");
48 this._partsToAdd.Add(part);
51 this._readOnlyPartsToAdd = this._partsToAdd.AsReadOnly();
53 this._partsToRemove = new List<ComposablePart>();
54 if (partsToRemove != null)
56 foreach (var part in partsToRemove)
58 if (part == null)
60 throw ExceptionBuilder.CreateContainsNullElement("partsToRemove");
62 this._partsToRemove.Add(part);
65 this._readOnlyPartsToRemove = this._partsToRemove.AsReadOnly();
68 /// <summary>
69 /// Returns the collection of parts that will be added.
70 /// </summary>
71 /// <value>The parts to be added.</value>
72 public ReadOnlyCollection<ComposablePart> PartsToAdd
74 get
76 lock (this._lock)
78 this._copyNeededForAdd = true;
79 return this._readOnlyPartsToAdd;
84 /// <summary>
85 /// Returns the collection of parts that will be removed.
86 /// </summary>
87 /// <value>The parts to be removed.</value>
88 public ReadOnlyCollection<ComposablePart> PartsToRemove
90 get
92 lock (this._lock)
94 this._copyNeededForRemove = true;
95 return this._readOnlyPartsToRemove;
100 /// <summary>
101 /// Adds the specified part to the <see cref="CompositionBatch"/>.
102 /// </summary>
103 /// <param name="part">
104 /// The part.
105 /// </param>
106 /// <exception cref="ArgumentNullException">
107 /// <paramref name="part"/> is <see langword="null"/>.
108 /// </exception>
109 public void AddPart(ComposablePart part)
111 Requires.NotNull(part, "part");
112 lock (this._lock)
114 if (this._copyNeededForAdd)
116 this._partsToAdd = new List<ComposablePart>(this._partsToAdd);
117 this._readOnlyPartsToAdd = this._partsToAdd.AsReadOnly();
118 this._copyNeededForAdd = false;
120 this._partsToAdd.Add(part);
124 /// <summary>
125 /// Removes the specified part from the <see cref="CompositionBatch"/>.
126 /// </summary>
127 /// <param name="part">
128 /// The part.
129 /// </param>
130 /// <exception cref="ArgumentNullException">
131 /// <paramref name="part"/> is <see langword="null"/>.
132 /// </exception>
133 public void RemovePart(ComposablePart part)
135 Requires.NotNull(part, "part");
136 lock (this._lock)
138 if (this._copyNeededForRemove)
140 this._partsToRemove = new List<ComposablePart>(this._partsToRemove);
141 this._readOnlyPartsToRemove = this._partsToRemove.AsReadOnly();
142 this._copyNeededForRemove = false;
144 this._partsToRemove.Add(part);
148 /// <summary>
149 /// Adds the specified export to the <see cref="CompositionBatch"/>.
150 /// </summary>
151 /// <param name="export">
152 /// The <see cref="Export"/> to add to the <see cref="CompositionBatch"/>.
153 /// </param>
154 /// <returns>
155 /// A <see cref="ComposablePart"/> that can be used remove the <see cref="Export"/>
156 /// from the <see cref="CompositionBatch"/>.
157 /// </returns>
158 /// <exception cref="ArgumentNullException">
159 /// <paramref name="export"/> is <see langword="null"/>.
160 /// </exception>
161 /// <remarks>
162 /// </remarks>
163 public ComposablePart AddExport(Export export)
165 Requires.NotNull(export, "export");
167 ComposablePart part = new SingleExportComposablePart(export);
169 this.AddPart(part);
171 return part;