15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

768 ❘ ChaPTer 28 mAnAGed extensibility frAmewOrk<br />

catalog.Changed += (sender, e) = ><br />

{<br />

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

foreach (var definition in e.AddedDefinitions)<br />

{<br />

foreach (var metadata in definition.Metadata)<br />

{<br />

sb.AppendFormat(<br />

"added definition with metadata - key: {0}, " +<br />

"value: {1}\n", metadata.Key, metadata.Value);<br />

}<br />

}<br />

foreach (var definition in e.RemovedDefinitions)<br />

{<br />

foreach (var metadata in definition.Metadata)<br />

{<br />

sb.AppendFormat(<br />

"removed definition with metadata - key: {0}, " +<br />

"value: {1}\n", metadata.Key, metadata.Value);<br />

}<br />

}<br />

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

};<br />

//...<br />

code snippet WPFCalculator/MainWindow.xaml.cs<br />

To get immediate notifi cation of new add-ins loaded to a directory, you can use the<br />

System.IO.FileSystemWatcher to register for changes on the add-in directory, <strong>and</strong><br />

invoke the Refresh method of the DirectoryCatalog with the Changed event of the<br />

FileSystemWatcher.<br />

The CompositionContainer just needs a ComposablePartCatalog to fi nd parts. DirectoryCatalog<br />

derives from ComposablePartCatalog . Other catalogs are AssemblyCatalog , TypeCatalog , <strong>and</strong><br />

AggregateCatalog . Let ’ s compare all these catalogs:<br />

➤<br />

➤<br />

➤<br />

➤<br />

T h e DirectoryCatalog searches parts within a directory.<br />

T h e AssemblyCatalog searches for parts directly within a referenced assembly. Contrary<br />

to the DirectoryCatalog , where assemblies might change in the directory during runtime, the<br />

AssemblyCatalog is immutable <strong>and</strong> parts cannot change.<br />

T h e TypeCatalog searches for imports within a list of types. IEnumerable < Type > can be passed to<br />

the constructor of this catalog.<br />

T h e AggregateCatalog is a catalog of catalogs. This catalog can be created from multiple<br />

ComposablePartCatalog objects <strong>and</strong> searches in all these catalogs. For example, you can create<br />

an AssemblyCatalog to search for imports within an assembly, two DirectoryCatalog objects to<br />

search in two different directories, <strong>and</strong> an AggregateCatalog that combines the three catalogs for<br />

import searches.<br />

When running the sample application (see Figure 28 - 4), the SimpleCalculator add - in is loaded, <strong>and</strong> you<br />

can do some calculations with operations supported by the add - in. From the AddIns menu, you can start<br />

add - ins that implement the interface ICalculatorExtender <strong>and</strong> see the user interface from these add -<br />

ins in the tab control. Information about exports <strong>and</strong> changes in the directory catalog is shown with the<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!