CompositionContainer 类

定义

管理部件的组合。Manages the composition of parts.

public ref class CompositionContainer : System::ComponentModel::Composition::Hosting::ExportProvider, IDisposable, System::ComponentModel::Composition::ICompositionService
public class CompositionContainer : System.ComponentModel.Composition.Hosting.ExportProvider, IDisposable, System.ComponentModel.Composition.ICompositionService
type CompositionContainer = class
    inherit ExportProvider
    interface ICompositionService
    interface IDisposable
Public Class CompositionContainer
Inherits ExportProvider
Implements ICompositionService, IDisposable
继承
CompositionContainer
实现

示例

在下面的示例中,使用目录初始化 CompositionContainer 对象,并用于填充部件的导入。In the following example, a CompositionContainer object is initialized with a catalog and is used to fill the imports of a part. 此示例使用特性化编程模型。This example uses the Attributed Programming Model.

[Export]
class MyAddin
{
    public String myData { get { return "The data!"; } }
}

class MyProgram
{
    [Import]
    public MyAddin myAddin { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        AggregateCatalog catalog = new AggregateCatalog();
        catalog.Catalogs.Add(new AssemblyCatalog(typeof(MyAddin).Assembly));
        CompositionContainer _container = new CompositionContainer(catalog);
        MyProgram myProgram = new MyProgram();
        _container.SatisfyImportsOnce(myProgram);
        Console.WriteLine(myProgram.myAddin.myData);
        Console.ReadLine();

        _container.Dispose();
    }
}
<Export()>
Public Class MyAddin
    Public ReadOnly Property theData As String
        Get
            Return "The Data!"
        End Get
    End Property
End Class

Public Class MyProgam
    Private _MyAddin As MyAddin

    <Import()>
    Public Property MyAddinProperty As MyAddin
        Get
            Return _MyAddin
        End Get
        Set(ByVal value As MyAddin)
            _MyAddin = value
        End Set
    End Property

End Class



Sub Main()
    Dim catalog As AggregateCatalog = New AggregateCatalog()
    catalog.Catalogs.Add(New AssemblyCatalog(GetType(MyAddin).Assembly))
    Dim container As CompositionContainer = New CompositionContainer(catalog)
    Dim theProgam As MyProgam = New MyProgam()
    container.SatisfyImportsOnce(theProgam)
    Console.WriteLine(theProgam.MyAddinProperty.theData)
    Console.ReadLine()

    container.Dispose()

End Sub

注解

CompositionContainer 对象在应用程序中有两个主要用途。A CompositionContainer object serves two major purposes in an application. 首先,它将跟踪可用于组合的部件及其依赖项,并在每次可用部件发生更改时执行组合。First, it keeps track of which parts are available for composition and what their dependencies are, and performs composition whenever the set of available parts changes. 其次,它提供了一些方法,应用程序可通过这些方法获取组合部件的实例,或填充可组合部件的依赖项。Second, it provides the methods by which the application gets instances of composed parts or fills the dependencies of a composable part.

重要

此类型实现 IDisposable 接口。This type implements the IDisposable interface. 在使用完类型后,您应直接或间接释放类型。When you have finished using the type, you should dispose of it either directly or indirectly. 若要直接释放类型,请在 Disposetry/ 块中调用其 catch 方法。To dispose of the type directly, call its Dispose method in a try/catch block. 若要间接释放类型,请使用 using(在 C# 中)或 Using(在 Visual Basic 中)等语言构造。To dispose of it indirectly, use a language construct such as using (in C#) or Using (in Visual Basic). 有关详细信息,请参阅 IDisposable 接口主题中的“使用实现 IDisposable 的对象”一节。For more information, see the "Using an Object that Implements IDisposable" section in the IDisposable interface topic.

部件可以直接或通过 Catalog 属性提供给容器。Parts can be made available to the container either directly or through the Catalog property. ComposablePartCatalog 中可发现的所有部件均可供容器用于实现导入,并直接添加任何部分。All the parts discoverable in this ComposablePartCatalog are available to the container to fulfill imports, along with any parts added directly.

Compose 方法允许将实例化部件添加到现有容器。The Compose method allows instantiated parts to be added to an existing container. 假设组合成功,这些部件将使用从容器检索的部件填充其导入,并将其导出提供给其他部件。Assuming composition is successful, these parts will have their imports filled with parts retrieved from the container, and their exports will be available to other parts. 标记为可重新组合的导入将注册以进行重新组合。Imports marked as recomposable will be registered for recomposition.

使用 SatisfyImportsOnce 方法,部件可以在不添加到容器的情况下填充其导入。The SatisfyImportsOnce method allows a part to have its imports filled without being added to the container. 如果组合成功,将填充部件的导入,但部件的导出将不可用于其他部件,并且将不会为重新组合注册任何导入。If the composition is successful, the part's imports will be filled, but the part's exports will not be available to other parts and no imports will be registered for recomposition.

应始终释放 CompositionContainer 对象。CompositionContainer objects should always be disposed. 调用 Dispose 方法时,CompositionContainer 对象还会释放已创建的所有部分。When the Dispose method is called, the CompositionContainer object also disposes all the parts that it has created.

可以从多个线程访问的 CompositionContainer 对象必须使用 CompositionContainer(ComposablePartCatalog, Boolean, ExportProvider[]) 构造函数设置为 trueisThreadSafe 参数进行构造。A CompositionContainer object that can be accessed from multiple threads must be constructed with the isThreadSafe parameter set to true, using the CompositionContainer(ComposablePartCatalog, Boolean, ExportProvider[]) constructor. true``isThreadSafe 时,性能会略微降低,因此,我们建议你将此参数设置为在单线程方案中 falsePerformance will be slightly slower when isThreadSafe is true, so we recommend that you set this parameter to false in single-threaded scenarios. 默认值为 falseThe default is false.

警告

CompositionContainer 决不会导入自身,也不应导入具有对它的引用的部分。A CompositionContainer should never import itself, or a part that has a reference to it. 此类引用可能会允许不受信任的部分访问容器中的所有部件。Such a reference could allow an untrusted part to gain access all the parts in the container.

构造函数

CompositionContainer()

初始化 CompositionContainer 类的新实例。Initializes a new instance of the CompositionContainer class.

CompositionContainer(ComposablePartCatalog, Boolean, ExportProvider[])

使用指定的目录、线程安全模式和导出提供程序初始化 CompositionContainer 类的新实例。Initializes a new instance of the CompositionContainer class with the specified catalog, thread-safe mode, and export providers.

CompositionContainer(ComposablePartCatalog, CompositionOptions, ExportProvider[])

使用指定的目录,选项和导出提供程序初始化 CompositionContainer 类的新实例。Initializes a new instance of the CompositionContainer class with the specified catalog, options, and export providers.

CompositionContainer(ComposablePartCatalog, ExportProvider[])

使用指定的目录和导出提供程序初始化 CompositionContainer 类的新实例。Initializes a new instance of the CompositionContainer class with the specified catalog and export providers.

CompositionContainer(CompositionOptions, ExportProvider[])

使用指定的选项和导出提供程序初始化 CompositionContainer 类的新实例。Initializes a new instance of the CompositionContainer class with the specified export providers and options.

CompositionContainer(ExportProvider[])

使用指定的导出提供程序初始化 CompositionContainer 类的新实例。Initializes a new instance of the CompositionContainer class with the specified export providers.

属性

Catalog

获取提供对 ComposablePartCatalog 对象的容器访问的 ExportGets the ComposablePartCatalog that provides the container access to Export objects.

Providers

获取提供对其他 ComposablePartCatalog 对象的容器访问的导出提供程序。Gets the export providers that provide the container access to additional ComposablePartCatalog objects.

方法

Compose(CompositionBatch)

在容器中添加或移除指定的 CompositionBatch 中的部件并执行组合。Adds or removes the parts in the specified CompositionBatch from the container and executes composition.

Dispose()

释放 CompositionContainer 类的当前实例所使用的所有资源。Releases all resources used by the current instance of the CompositionContainer class.

Dispose(Boolean)

释放 CompositionContainer 使用的非托管资源,并选择性地释放托管资源。Releases the unmanaged resources used by the CompositionContainer and optionally releases the managed resources.

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetExport<T,TMetadataView>()

返回具有从指定的类型参数派生的协定名称的导出。Returns the export with the contract name derived from the specified type parameter. 如果不是正好有一个匹配的导出,则将引发异常。If there is not exactly one matching export, an exception is thrown.

(继承自 ExportProvider)
GetExport<T,TMetadataView>(String)

返回具有指定的协定名称的导出。Returns the export with the specified contract name. 如果不是正好有一个匹配的导出,则将引发异常。If there is not exactly one matching export, an exception is thrown.

(继承自 ExportProvider)
GetExport<T>()

返回具有从指定的类型参数派生的协定名称的导出。Returns the export with the contract name derived from the specified type parameter. 如果不是正好有一个匹配的导出,则将引发异常。If there is not exactly one matching export, an exception is thrown.

(继承自 ExportProvider)
GetExport<T>(String)

返回具有指定的协定名称的导出。Returns the export with the specified contract name. 如果不是正好有一个匹配的导出,则将引发异常。If there is not exactly one matching export, an exception is thrown.

(继承自 ExportProvider)
GetExportedValue<T>()

返回具有从指定的类型参数派生的协定名称的已导出对象。Returns the exported object with the contract name derived from the specified type parameter. 如果不是正好有一个匹配的已导出对象,则将引发异常。If there is not exactly one matching exported object, an exception is thrown.

(继承自 ExportProvider)
GetExportedValue<T>(String)

返回具有指定的协定名称的已导出对象。Returns the exported object with the specified contract name. 如果不是正好有一个匹配的已导出对象,则将引发异常。If there is not exactly one matching exported object, an exception is thrown.

(继承自 ExportProvider)
GetExportedValueOrDefault<T>()

获取具有从指定的类型参数派生的协定名称的已导出对象或指定类型的默认值,如果有多个匹配的已导出对象,则引发异常。Gets the exported object with the contract name derived from the specified type parameter or the default value for the specified type, or throws an exception if there is more than one matching exported object.

(继承自 ExportProvider)
GetExportedValueOrDefault<T>(String)

获取具有指定的协定名称的已导出对象或指定类型的默认值,如果有多个匹配的已导出对象,则引发异常。Gets the exported object with the specified contract name or the default value for the specified type, or throws an exception if there is more than one matching exported object.

(继承自 ExportProvider)
GetExportedValues<T>()

获取具有从指定的类型参数派生的协定名称的所有已导出对象。Gets all the exported objects with the contract name derived from the specified type parameter.

(继承自 ExportProvider)
GetExportedValues<T>(String)

获取具有指定的协定名称的所有已导出对象。Gets all the exported objects with the specified contract name.

(继承自 ExportProvider)
GetExports(ImportDefinition)

获取与指定的导入定义条件匹配的所有导出。Gets all exports that match the conditions of the specified import definition.

(继承自 ExportProvider)
GetExports(ImportDefinition, AtomicComposition)

获取与指定的导入定义和组合条件匹配的所有导出。Gets all exports that match the conditions of the specified import definition and composition.

(继承自 ExportProvider)
GetExports(Type, Type, String)

获取具有指定的协定名称的所有导出。Gets all the exports with the specified contract name.

(继承自 ExportProvider)
GetExports<T,TMetadataView>()

获取具有从指定的类型参数派生的协定名称的所有导出。Gets all the exports with the contract name derived from the specified type parameter.

(继承自 ExportProvider)
GetExports<T,TMetadataView>(String)

获取具有指定的协定名称的所有导出。Gets all the exports with the specified contract name.

(继承自 ExportProvider)
GetExports<T>()

获取具有从指定的类型参数派生的协定名称的所有导出。Gets all the exports with the contract name derived from the specified type parameter.

(继承自 ExportProvider)
GetExports<T>(String)

获取具有指定的协定名称的所有导出。Gets all the exports with the specified contract name.

(继承自 ExportProvider)
GetExportsCore(ImportDefinition, AtomicComposition)

返回与指定的 ImportDefinition 对象中的条件相匹配的所有导出的集合。Returns a collection of all exports that match the conditions in the specified ImportDefinition object.

GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
OnExportsChanged(ExportsChangeEventArgs)

引发 ExportsChanged 事件。Raises the ExportsChanged event.

(继承自 ExportProvider)
OnExportsChanging(ExportsChangeEventArgs)

引发 ExportsChanging 事件。Raises the ExportsChanging event.

(继承自 ExportProvider)
ReleaseExport(Export)

Export 中释放指定的 CompositionContainer 对象。Releases the specified Export object from the CompositionContainer.

ReleaseExport<T>(Lazy<T>)

从组合中移除指定的导出并释放其资源(如果可能)。Removes the specified export from composition and releases its resources if possible.

ReleaseExports(IEnumerable<Export>)

Export 中释放一组 CompositionContainer 对象。Releases a set of Export objects from the CompositionContainer.

ReleaseExports<T,TMetadataView>(IEnumerable<Lazy<T,TMetadataView>>)

从组合中移除导出的集合并释放其资源(如果可能)。Removes a collection of exports from composition and releases their resources if possible.

ReleaseExports<T>(IEnumerable<Lazy<T>>)

从组合中移除导出的集合并释放其资源(如果可能)。Removes a collection of exports from composition and releases their resources if possible.

SatisfyImportsOnce(ComposablePart)

满足指定的 ComposablePart 对象的导入,而无需注册该对象以进行重新组合。Satisfies the imports of the specified ComposablePart object without registering it for recomposition.

ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)
TryGetExports(ImportDefinition, AtomicComposition, IEnumerable<Export>)

获取与指定的导入条件匹配的所有导出。Gets all the exports that match the conditions of the specified import.

(继承自 ExportProvider)

事件

ExportsChanged

ExportProvider 中的导出更改时发生。Occurs when the exports in the ExportProvider change.

(继承自 ExportProvider)
ExportsChanging

当提供的导出正在更改时发生。Occurs when the provided exports are changing.

(继承自 ExportProvider)

扩展方法

ComposeExportedValue<T>(CompositionContainer, T)

从指定的值创建一个部件,并在指定的组合容器中组合该部件。Creates a part from the specified value and composes it in the specified composition container.

ComposeExportedValue<T>(CompositionContainer, String, T)

从指定协定名称下的指定对象创建一个部件,并在指定的组合容器中组合该部件。Creates a part from the specified object under the specified contract name and composes it in the specified composition container.

ComposeParts(CompositionContainer, Object[])

从特性化对象的数组创建可组合部件,并在指定的组合容器中组合这些部件。Creates composable parts from an array of attributed objects and composes them in the specified composition container.

SatisfyImportsOnce(ICompositionService, Object)

通过使用指定的组合服务并禁用重新组合来组合指定的部件。Composes the specified part by using the specified composition service, with recomposition disabled.

SatisfyImportsOnce(ICompositionService, Object, ReflectionContext)

通过使用指定的组合服务并禁用重新组合及使用指定的反射上下文来组合指定的部件。Composes the specified part by using the specified composition service, with recomposition disabled and using the specified reflection context.

适用于

另请参阅