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. 若要直接處置型別,請呼叫其 try/catch 區塊中的 Dispose 方法。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. 標示為 recomposable 的匯入將會註冊進行重新組合。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. 呼叫方法時CompositionContainer , 物件也會處置它所建立的所有元件。 DisposeWhen the Dispose method is called, the CompositionContainer object also disposes all the parts that it has created.

可以從多個執行緒存取的isThreadSafe true CompositionContainer(ComposablePartCatalog, Boolean, ExportProvider[]) CompositionContainer物件, 必須使用此函式, 以設定為的參數來加以結構化。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. 當為isThreadSafe true時, 效能會稍微變慢, 因此建議您在單一執行緒的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.

警告

絕對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,其可提供容器對 Export 物件的存取。Gets 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 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。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 的淺層複本 (Shallow Copy)。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()

傳回代表目前物件的字串。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.

適用於

另請參閱