Updated: October 2010
Discovers attributed parts in the assemblies in a specified directory.
Public Class DirectoryCatalog _ Inherits ComposablePartCatalog _ Implements INotifyComposablePartCatalogChanged, ICompositionElement
public class DirectoryCatalog : ComposablePartCatalog, INotifyComposablePartCatalogChanged, ICompositionElement
public ref class DirectoryCatalog : public ComposablePartCatalog, INotifyComposablePartCatalogChanged, ICompositionElement
type DirectoryCatalog = class inherit ComposablePartCatalog interface INotifyComposablePartCatalogChanged interface ICompositionElement end
The DirectoryCatalog type exposes the following members.
A DirectoryCatalog parses the contents of the designated directory. Any attributed parts contained in DLL files are extracted and made available via the catalog. A search pattern, to restrict parsing to specific DLLs, can also be specified using the same syntax as GetFiles.
The designated directory should not allow access to non-administrators. For example, using a folder that contains temporary Internet files could create vulnerabilities in the application.
The following example creates a DirectoryCatalog that searches the directory the application runs from for parts. It uses a simple import to test the catalog. In order to fulfill this import, a DLL in the directory must have a matching export. (See below.)
Public Class Test2 <Import()> Public Property data As Test1 End Class Sub Main() Dim catalog As New DirectoryCatalog(".") Dim container As New CompositionContainer(catalog) Dim test As New Test2() container.SatisfyImportsOnce(test) Console.WriteLine(test.data.data) Console.ReadLine() End Sub
public class Test2 { [Import] public Test1 data { get; set; } } class Program { static void Main(string[] args) { DirectoryCatalog catalog = new DirectoryCatalog("."); CompositionContainer container = new CompositionContainer(catalog); Test2 test = new Test2(); container.SatisfyImportsOnce(test); Console.WriteLine(test.data.data); Console.ReadLine(); } }
To create the matching export, the following code must be in a DLL file. To create a separate DLL in Visual Studio, add a new project of the type "Class Library" to the solution, and put this code in it.
<Export()> Public Class Test1 Public ReadOnly Property data As String Get Return "The data!" End Get End Property End Class
[Export] public class Test1 { public String data = "The data!"; }
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
Date
History
Reason
October 2010
Added note about potential security vulnerabilities.
Content bug fix.