Click to Rate and Give Feedback
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2010/.NET Framework 4

Other versions are also available for the following:
.NET Framework Class Library
DirectoryCatalog Class

Updated: October 2010

Discovers attributed parts in the assemblies in a specified directory.

System..::.Object
  System.ComponentModel.Composition.Primitives..::.ComposablePartCatalog
    System.ComponentModel.Composition.Hosting..::.DirectoryCatalog

Namespace:  System.ComponentModel.Composition.Hosting
Assembly:  System.ComponentModel.Composition (in System.ComponentModel.Composition.dll)
Visual Basic
Public Class DirectoryCatalog _
    Inherits ComposablePartCatalog _
    Implements INotifyComposablePartCatalogChanged, ICompositionElement
C#
public class DirectoryCatalog : ComposablePartCatalog, 
    INotifyComposablePartCatalogChanged, ICompositionElement
Visual C++
public ref class DirectoryCatalog : public ComposablePartCatalog, 
    INotifyComposablePartCatalogChanged, ICompositionElement
F#
type DirectoryCatalog =  
    class
        inherit ComposablePartCatalog
        interface INotifyComposablePartCatalogChanged
        interface ICompositionElement
    end

The DirectoryCatalog type exposes the following members.

  NameDescription
Public methodDirectoryCatalog(String)Initializes a new instance of the DirectoryCatalog class with ComposablePartDefinition objects based on all the DLL files in the specified directory path.
Public methodDirectoryCatalog(String, String)Initializes a new instance of the DirectoryCatalog class with ComposablePartDefinition objects based on the specified search pattern in the specified directory path.
Top
  NameDescription
Public propertyFullPathGets the translated absolute path observed by the DirectoryCatalog.
Public propertyLoadedFilesGets the collection of files currently loaded in the catalog.
Public propertyPartsGets the part definitions that are contained in the directory catalog. (Overrides ComposablePartCatalog..::.Parts.)
Public propertyPathGets the path observed by the DirectoryCatalog.
Public propertySearchPatternGets the search pattern passed into the constructor of DirectoryCatalog.
Top
  NameDescription
Public methodDispose()()()Releases all resources used by the ComposablePartCatalog. (Inherited from ComposablePartCatalog.)
Protected methodDispose(Boolean)Releases the unmanaged resources used by the DirectoryCatalog and optionally releases the managed resources. (Overrides ComposablePartCatalog..::.Dispose(Boolean).)
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetExportsGets the export definitions that match the constraint expressed by the specified definition. (Overrides ComposablePartCatalog..::.GetExports(ImportDefinition).)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Protected methodOnChangedRaises the Changed event.
Protected methodOnChangingRaises the Changing event.
Public methodRefreshRefreshes the ComposablePartDefinition objects with the latest files in the directory that match the search pattern.
Public methodToStringGets a string representation of the directory catalog. (Overrides Object..::.ToString()()().)
Top
  NameDescription
Public eventChangedOccurs when the contents of the catalog has changed.
Public eventChangingOccurs when the catalog is changing.
Top
  NameDescription
Explicit interface implemetationPrivate propertyICompositionElement..::.DisplayNameGets the display name of the directory catalog.
Explicit interface implemetationPrivate propertyICompositionElement..::.OriginGets the composition element from which the directory catalog originated.
Top

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.

Caution noteCaution

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.)

Visual Basic
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
C#
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.

Visual Basic
<Export()>
Public Class Test1
    Public ReadOnly Property data As String
        Get
            Return "The data!"
        End Get
    End Property
End Class
C#
[Export]
public class Test1
{
    public String data = "The data!";
}

.NET Framework

Supported in: 4

.NET Framework Client Profile

Supported in: 4

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

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Date

History

Reason

October 2010

Added note about potential security vulnerabilities.

Content bug fix.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2012 Microsoft. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker