15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

C a t a l o g s ❘ 767<br />

To remove exports dynamically, the Unchecked event is set in the CheckBox control that ’ s within<br />

the MenuItem header. The ICalculatorExtension can be accessed from the Tag property of the<br />

MenuItem control because it was set in the previous code snippet. To remove a part from the container,<br />

a CompositeBatch object must be created that contains parts for removal. The RemovePart() method of<br />

the CompositeBatch class requires a ComposablePart object; there ’ s no overload for attributed objects.<br />

With the help of the class AttributedModelServices from an attributed object, a ComposablePart can be<br />

created with the CreatePart() method. The CompositonBatch is then passed to the Compose() method of<br />

the CompositionContainer to remove the part from it:<br />

menuCheck.Unchecked += (sender1, e1) = ><br />

{<br />

MenuItem mi = (sender1 as CheckBox).Tag as MenuItem;<br />

ICalculatorExtension ext = mi.Tag as ICalculatorExtension;<br />

ComposablePart part =<br />

AttributedModelServices.CreatePart(ext);<br />

var batch = new CompositionBatch();<br />

batch.RemovePart(part);<br />

container.Compose(batch);<br />

MenuItem parentMenu = mi.Parent as MenuItem;<br />

parentMenu.Items.Remove(mi);<br />

};<br />

Removing parts is only possible if the AllowRecomposition property of the Import<br />

attribute is set to true.<br />

With an export provider, you can get information on exports added <strong>and</strong> removed by implementing a h<strong>and</strong>ler<br />

to the ExportsChanged event. The parameter e of type ExportsChangedEventArgs contains a list of added<br />

exports <strong>and</strong> removed exports that are written to a TextBlock control:<br />

var container = new CompositionContainer(catalog);<br />

container.ExportsChanged += (sender, e) = ><br />

{<br />

var sb = new StringBuilder();<br />

};<br />

foreach (var item in e.AddedExports)<br />

{<br />

sb.AppendFormat("added export {0}\n", item.ContractName);<br />

}<br />

foreach (var item in e.RemovedExports)<br />

{<br />

sb.AppendFormat("removed export {0}\n", item.ContractName);<br />

}<br />

this.textStatus.Text += sb.ToString();<br />

CaTalogs<br />

A catalog defi nes where MEF searches for requested parts. The sample application uses a DirectoryCatalog<br />

to load the assemblies with parts from a specifi ed directory. With the DirectoryCatalog , you can get<br />

change information with the Changed event <strong>and</strong> iterate through all added <strong>and</strong> removed defi nitions. The<br />

DirectoryCatalog does not itself register to fi le system changes. Instead, you need to invoke the Refresh()<br />

method of the DirectoryCatalog , <strong>and</strong> if changes were made since the last read, the Changing <strong>and</strong> Changed<br />

events are fi red.<br />

private void InitializeContainer()<br />

{<br />

catalog = new DirectoryCatalog(Properties.Settings.Default.AddInDirectory);<br />

container = new CompositionContainer(catalog);<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!