CompositionContainer CompositionContainer CompositionContainer CompositionContainer Class

定義

管理組件的撰寫。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
繼承
CompositionContainerCompositionContainerCompositionContainerCompositionContainer
實作

範例

在下列範例中,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

備註

ACompositionContainer物件會有兩個主要的目的應用程式中。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. 若要直接處置型別,呼叫其Dispose方法中的try / 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.

ACompositionContainer可從多個執行緒存取的物件必須以建構isThreadSafe參數設為true,並使用CompositionContainer(ComposablePartCatalog, Boolean, ExportProvider[])建構函式。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. 效能會稍微慢一點 whenisThreadSafetrue,因此我們建議您將此參數設定為false在單一執行緒的情況下。Performance 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.

警告

ACompositionContainer本身,或具有對它的參考組件,應該永遠不會匯入。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() CompositionContainer() CompositionContainer()

初始化 CompositionContainer 類別的新執行個體。Initializes a new instance of the CompositionContainer class.

CompositionContainer(ComposablePartCatalog, Boolean, ExportProvider[]) CompositionContainer(ComposablePartCatalog, Boolean, ExportProvider[]) CompositionContainer(ComposablePartCatalog, Boolean, ExportProvider[]) 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(ComposablePartCatalog, CompositionOptions, ExportProvider[]) CompositionContainer(ComposablePartCatalog, CompositionOptions, ExportProvider[]) 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(ComposablePartCatalog, ExportProvider[]) CompositionContainer(ComposablePartCatalog, ExportProvider[]) CompositionContainer(ComposablePartCatalog, ExportProvider[])

使用指定的目錄和匯出提供者,初始化 CompositionContainer 類別的新執行個體。Initializes a new instance of the CompositionContainer class with the specified catalog and export providers.

CompositionContainer(CompositionOptions, ExportProvider[]) CompositionContainer(CompositionOptions, ExportProvider[]) CompositionContainer(CompositionOptions, ExportProvider[]) CompositionContainer(CompositionOptions, ExportProvider[])

使用指定的匯出提供者和選項,初始化 CompositionContainer 類別的新執行個體。Initializes a new instance of the CompositionContainer class with the specified export providers and options.

CompositionContainer(ExportProvider[]) CompositionContainer(ExportProvider[]) CompositionContainer(ExportProvider[]) CompositionContainer(ExportProvider[])

使用指定的匯出提供者,初始化 CompositionContainer 類別的新執行個體。Initializes a new instance of the CompositionContainer class with the specified export providers.

屬性

Catalog Catalog Catalog Catalog

取得 ComposablePartCatalog,其可提供容器對 Export 物件的存取。Gets the ComposablePartCatalog that provides the container access to Export objects.

Providers Providers Providers Providers

取得匯出提供者,這些提供者可提供容器對額外 ComposablePartCatalog 物件的存取。Gets the export providers that provide the container access to additional ComposablePartCatalog objects.

方法

Compose(CompositionBatch) Compose(CompositionBatch) Compose(CompositionBatch) Compose(CompositionBatch)

在容器中加入或移除指定之 CompositionBatch 的組件,然後執行撰寫。Adds or removes the parts in the specified CompositionBatch from the container and executes composition.

Dispose() Dispose() Dispose() Dispose()

釋放 CompositionContainer 類別目前的執行個體所使用的全部資源。Releases all resources used by the current instance of the CompositionContainer class.

Dispose(Boolean) Dispose(Boolean) Dispose(Boolean) Dispose(Boolean)

釋放 CompositionContainer 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。Releases the unmanaged resources used by the CompositionContainer and optionally releases the managed resources.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetExport<T,TMetadataView>() GetExport<T,TMetadataView>() GetExport<T,TMetadataView>() 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.

(Inherited from ExportProvider)
GetExport<T,TMetadataView>(String) GetExport<T,TMetadataView>(String) GetExport<T,TMetadataView>(String) GetExport<T,TMetadataView>(String)

傳回具有指定合約名稱的匯出。Returns the export with the specified contract name. 如果沒有任何項目完全符合匯出結果,則擲回例外狀況。If there is not exactly one matching export, an exception is thrown.

(Inherited from ExportProvider)
GetExport<T>() GetExport<T>() GetExport<T>() 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.

(Inherited from ExportProvider)
GetExport<T>(String) GetExport<T>(String) GetExport<T>(String) GetExport<T>(String)

傳回具有指定合約名稱的匯出。Returns the export with the specified contract name. 如果沒有任何項目完全符合匯出結果,則擲回例外狀況。If there is not exactly one matching export, an exception is thrown.

(Inherited from ExportProvider)
GetExportedValue<T>() GetExportedValue<T>() GetExportedValue<T>() 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.

(Inherited from ExportProvider)
GetExportedValue<T>(String) GetExportedValue<T>(String) GetExportedValue<T>(String) 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.

(Inherited from ExportProvider)
GetExportedValueOrDefault<T>() GetExportedValueOrDefault<T>() GetExportedValueOrDefault<T>() 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.

(Inherited from ExportProvider)
GetExportedValueOrDefault<T>(String) GetExportedValueOrDefault<T>(String) GetExportedValueOrDefault<T>(String) 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.

(Inherited from ExportProvider)
GetExportedValues<T>() GetExportedValues<T>() GetExportedValues<T>() GetExportedValues<T>()

取得具有從指定型別參數中衍生之合約名稱的匯出物件。Gets all the exported objects with the contract name derived from the specified type parameter.

(Inherited from ExportProvider)
GetExportedValues<T>(String) GetExportedValues<T>(String) GetExportedValues<T>(String) GetExportedValues<T>(String)

取得具有指定合約名稱的所有匯出物件。Gets all the exported objects with the specified contract name.

(Inherited from ExportProvider)
GetExports(ImportDefinition) GetExports(ImportDefinition) GetExports(ImportDefinition) GetExports(ImportDefinition)

取得符合指定之匯入定義條件的所有匯出。Gets all exports that match the conditions of the specified import definition.

(Inherited from ExportProvider)
GetExports(ImportDefinition, AtomicComposition) GetExports(ImportDefinition, AtomicComposition) GetExports(ImportDefinition, AtomicComposition) GetExports(ImportDefinition, AtomicComposition)

取得符合指定匯入定義和撰寫條件的所有匯出。Gets all exports that match the conditions of the specified import definition and composition.

(Inherited from ExportProvider)
GetExports(Type, Type, String) GetExports(Type, Type, String) GetExports(Type, Type, String) GetExports(Type, Type, String)

取得具有指定之合約名稱的所有匯出結果。Gets all the exports with the specified contract name.

(Inherited from ExportProvider)
GetExports<T,TMetadataView>() GetExports<T,TMetadataView>() GetExports<T,TMetadataView>() GetExports<T,TMetadataView>()

取得具有從指定型別參數衍生之合約名稱的所有匯出。Gets all the exports with the contract name derived from the specified type parameter.

(Inherited from ExportProvider)
GetExports<T,TMetadataView>(String) GetExports<T,TMetadataView>(String) GetExports<T,TMetadataView>(String) GetExports<T,TMetadataView>(String)

取得具有指定之合約名稱的所有匯出結果。Gets all the exports with the specified contract name.

(Inherited from ExportProvider)
GetExports<T>() GetExports<T>() GetExports<T>() GetExports<T>()

取得具有從指定型別參數衍生之合約名稱的所有匯出。Gets all the exports with the contract name derived from the specified type parameter.

(Inherited from ExportProvider)
GetExports<T>(String) GetExports<T>(String) GetExports<T>(String) GetExports<T>(String)

取得具有指定之合約名稱的所有匯出結果。Gets all the exports with the specified contract name.

(Inherited from ExportProvider)
GetExportsCore(ImportDefinition, AtomicComposition) GetExportsCore(ImportDefinition, AtomicComposition) GetExportsCore(ImportDefinition, AtomicComposition) GetExportsCore(ImportDefinition, AtomicComposition)

傳回符合指定 ImportDefinition 物件之條件的所有匯出集合。Returns a collection of all exports that match the conditions in the specified ImportDefinition object.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
OnExportsChanged(ExportsChangeEventArgs) OnExportsChanged(ExportsChangeEventArgs) OnExportsChanged(ExportsChangeEventArgs) OnExportsChanged(ExportsChangeEventArgs)

引發 ExportsChanged 事件。Raises the ExportsChanged event.

(Inherited from ExportProvider)
OnExportsChanging(ExportsChangeEventArgs) OnExportsChanging(ExportsChangeEventArgs) OnExportsChanging(ExportsChangeEventArgs) OnExportsChanging(ExportsChangeEventArgs)

引發 ExportsChanging 事件。Raises the ExportsChanging event.

(Inherited from ExportProvider)
ReleaseExport(Export) ReleaseExport(Export) ReleaseExport(Export) ReleaseExport(Export)

Export 釋放指定的 CompositionContainer 物件。Releases the specified Export object from the CompositionContainer.

ReleaseExport<T>(Lazy<T>) ReleaseExport<T>(Lazy<T>) ReleaseExport<T>(Lazy<T>) ReleaseExport<T>(Lazy<T>)

從複合移除指定的匯出結果,可能的話,亦釋放其資源。Removes the specified export from composition and releases its resources if possible.

ReleaseExports(IEnumerable<Export>) ReleaseExports(IEnumerable<Export>) ReleaseExports(IEnumerable<Export>) ReleaseExports(IEnumerable<Export>)

Export 釋放一組 CompositionContainer 物件。Releases a set of Export objects from the CompositionContainer.

ReleaseExports<T,TMetadataView>(IEnumerable<Lazy<T,TMetadataView>>) ReleaseExports<T,TMetadataView>(IEnumerable<Lazy<T,TMetadataView>>) ReleaseExports<T,TMetadataView>(IEnumerable<Lazy<T,TMetadataView>>) 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>>) ReleaseExports<T>(IEnumerable<Lazy<T>>) ReleaseExports<T>(IEnumerable<Lazy<T>>) ReleaseExports<T>(IEnumerable<Lazy<T>>)

從複合移除匯出結果的集合,可能的話,亦釋放其資源。Removes a collection of exports from composition and releases their resources if possible.

SatisfyImportsOnce(ComposablePart) SatisfyImportsOnce(ComposablePart) SatisfyImportsOnce(ComposablePart) SatisfyImportsOnce(ComposablePart)

滿足指定之 ComposablePart 物件的匯入,而不需註冊重新撰寫。Satisfies the imports of the specified ComposablePart object without registering it for recomposition.

ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)
TryGetExports(ImportDefinition, AtomicComposition, IEnumerable<Export>) TryGetExports(ImportDefinition, AtomicComposition, IEnumerable<Export>) TryGetExports(ImportDefinition, AtomicComposition, IEnumerable<Export>) TryGetExports(ImportDefinition, AtomicComposition, IEnumerable<Export>)

取得符合指定之匯入條件的所有匯出。Gets all the exports that match the conditions of the specified import.

(Inherited from ExportProvider)

事件

ExportsChanged ExportsChanged ExportsChanged ExportsChanged

ExportProvider 中的匯出變更時發生。Occurs when the exports in the ExportProvider change.

(Inherited from ExportProvider)
ExportsChanging ExportsChanging ExportsChanging ExportsChanging

當提供的匯出變更時發生。Occurs when the provided exports are changing.

(Inherited from ExportProvider)

Extension Methods

ComposeExportedValue<T>(CompositionContainer, T) ComposeExportedValue<T>(CompositionContainer, T) ComposeExportedValue<T>(CompositionContainer, T) ComposeExportedValue<T>(CompositionContainer, T)

從指定的值建立組件,並在指定的撰寫容器中撰寫它。Creates a part from the specified value and composes it in the specified composition container.

ComposeExportedValue<T>(CompositionContainer, String, T) ComposeExportedValue<T>(CompositionContainer, String, T) ComposeExportedValue<T>(CompositionContainer, String, T) 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[]) ComposeParts(CompositionContainer, Object[]) ComposeParts(CompositionContainer, Object[]) ComposeParts(CompositionContainer, Object[])

從屬性化物件的陣列建立可組合的組件,並在指定的撰寫容器中撰寫它們。Creates composable parts from an array of attributed objects and composes them in the specified composition container.

SatisfyImportsOnce(ICompositionService, Object) SatisfyImportsOnce(ICompositionService, Object) SatisfyImportsOnce(ICompositionService, Object) SatisfyImportsOnce(ICompositionService, Object)

藉由使用指定的撰寫服務時,停用重新撰寫的撰寫指定的組件。Composes the specified part by using the specified composition service, with recomposition disabled.

SatisfyImportsOnce(ICompositionService, Object, ReflectionContext) SatisfyImportsOnce(ICompositionService, Object, ReflectionContext) SatisfyImportsOnce(ICompositionService, Object, ReflectionContext) SatisfyImportsOnce(ICompositionService, Object, ReflectionContext)

使用指定的撰寫服務時,與重新撰寫已停用,並使用指定的反映內容撰寫指定的組件。Composes the specified part by using the specified composition service, with recomposition disabled and using the specified reflection context.

適用於

另請參閱