ExportAttribute クラス

定義

型、プロパティ、フィールド、またはメソッドが特定のエクスポートを提供することを指定します。

public ref class ExportAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Field | System.AttributeTargets.Method | System.AttributeTargets.Property, AllowMultiple=true, Inherited=false)]
public class ExportAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Field | System.AttributeTargets.Method | System.AttributeTargets.Property, AllowMultiple=true, Inherited=false)>]
type ExportAttribute = class
    inherit Attribute
Public Class ExportAttribute
Inherits Attribute
継承
ExportAttribute
派生
属性

次の例は、 で ExportAttribute装飾された 3 つのクラスと、それらに一致する 3 つのインポートを示しています。

//Default export infers type and contract name from the
//exported type.  This is the preferred method.
[Export]
public class MyExport1
{
    public String data = "Test Data 1.";
}

public class MyImporter1
{
    [Import]
    public MyExport1 importedMember { get; set; }
}

public interface MyInterface
{
}

//Specifying the contract type may be important if
//you want to export a type other then the base type,
//such as an interface.
[Export(typeof(MyInterface))]
public class MyExport2 : MyInterface
{
    public String data = "Test Data 2.";
}

public class MyImporter2
{
    //The import must match the contract type!
    [Import(typeof(MyInterface))]
    public MyExport2 importedMember { get; set; }
}

//Specifying a contract name should only be
//needed in rare caes. Usually, using metadata
//is a better approach.
[Export("MyContractName", typeof(MyInterface))]
public class MyExport3 : MyInterface
{
    public String data = "Test Data 3.";
}

public class MyImporter3
{
    //Both contract name and type must match!
    [Import("MyContractName", typeof(MyInterface))]
    public MyExport3 importedMember { get; set; }
}

class Program
{

    static void Main(string[] args)
    {
        AggregateCatalog catalog = new AggregateCatalog();
        catalog.Catalogs.Add(new AssemblyCatalog(typeof(MyExport1).Assembly));
        CompositionContainer _container = new CompositionContainer(catalog);
        MyImporter1 test1 = new MyImporter1();
        MyImporter2 test2 = new MyImporter2();
        MyImporter3 test3 = new MyImporter3();
        _container.SatisfyImportsOnce(test1);
        _container.SatisfyImportsOnce(test2);
        _container.SatisfyImportsOnce(test3);
        Console.WriteLine(test1.importedMember.data);
        Console.WriteLine(test2.importedMember.data);
        Console.WriteLine(test3.importedMember.data);
        Console.ReadLine();
    }
}
'Default export infers type and contract name from the
'exported type.  This is the preferred method.
<Export()>
Public Class MyExport1
    Public ReadOnly Property data As String
        Get
            Return "Test Data 1."
        End Get
    End Property
End Class

Public Class MyImporter1

    <Import()>
    Public Property ImportedMember As MyExport1

End Class

Public Interface MyInterface

End Interface

'Specifying the contract type may be important if
'you want to export a type other then the base type,
'such as an interface.
<Export(GetType(MyInterface))>
Public Class MyExport2
    Implements MyInterface
    Public ReadOnly Property data As String
        Get
            Return "Test Data 2."
        End Get
    End Property
End Class

Public Class MyImporter2
    'The import must match the contract type!
    <Import(GetType(MyInterface))>
    Public Property ImportedMember As MyExport2
End Class

'Specifying a contract name should only be 
'needed in rare caes. Usually, using metadata
'is a better approach.
<Export("MyContractName", GetType(MyInterface))>
Public Class MyExport3
    Implements MyInterface
    Public ReadOnly Property data As String
        Get
            Return "Test Data 3."
        End Get
    End Property
End Class

Public Class MyImporter3
    'Both contract name and type must match!
    <Import("MyContractName", GetType(MyInterface))>
    Public Property ImportedMember As MyExport3
End Class



Sub Main()
    Dim catalog As AggregateCatalog = New AggregateCatalog()
    catalog.Catalogs.Add(New AssemblyCatalog(GetType(MyExport1).Assembly))
    Dim container As CompositionContainer = New CompositionContainer(catalog)
    Dim test1 As MyImporter1 = New MyImporter1()
    Dim test2 As MyImporter2 = New MyImporter2()
    Dim test3 As MyImporter3 = New MyImporter3()
    container.SatisfyImportsOnce(test1)
    container.SatisfyImportsOnce(test2)
    container.SatisfyImportsOnce(test3)
    Console.WriteLine(test1.ImportedMember.data)
    Console.WriteLine(test2.ImportedMember.data)
    Console.WriteLine(test3.ImportedMember.data)
    Console.ReadLine()
End Sub

注釈

属性プログラミング モデルでは、 は、 ExportAttribute 特定のコントラクトを満たすオブジェクトをパーツがエクスポートするか、コンポジション コンテナーに提供することを宣言します。 コンポジション中に、一致するコントラクトを持つインポートを含むパーツは、エクスポートされたオブジェクトによってそれらの依存関係を満たします。

ExportAttribute 、クラス全体、またはクラスのプロパティ、フィールド、またはメソッドのいずれかを装飾できます。 クラス全体が修飾されている場合、クラスのインスタンスはエクスポートされたオブジェクトです。 クラスのメンバーが修飾されている場合、エクスポートされたオブジェクトは装飾されたメンバーの値になります。

コントラクトが一致するかどうかは、主にコントラクト名とコントラクトの種類によって決まります。 詳細については、「ImportAttribute」を参照してください。

コンストラクター

ExportAttribute()

ExportAttribute クラスの新しいインスタンスを初期化して、この属性でマークされた型またはメンバーを既定のコントラクト名でエクスポートします。

ExportAttribute(String)

ExportAttribute クラスの新しいインスタンスを初期化して、この属性でマークされた型またはメンバーを指定されたコントラクト名でエクスポートします。

ExportAttribute(String, Type)

ExportAttribute クラスの新しいインスタンスを初期化して、指定された型を指定されたコントラクト名でエクスポートします。

ExportAttribute(Type)

ExportAttribute クラスの新しいインスタンスを初期化して、この属性でマークされた型またはメンバーを、指定された型から派生したコントラクト名でエクスポートします。

プロパティ

ContractName

この属性でマークされた型またはメンバーをエクスポートするために使用されるコントラクト名を取得します。

ContractType

この属性がアタッチされたメンバーでエクスポートされるコントラクトの型を取得します。

TypeId

派生クラスで実装されると、この Attribute の一意の識別子を取得します。

(継承元 Attribute)

メソッド

Equals(Object)

このインスタンスが、指定されたオブジェクトと等価であるかどうかを示す値を返します。

(継承元 Attribute)
GetHashCode()

このインスタンスのハッシュ コードを返します。

(継承元 Attribute)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
IsDefaultAttribute()

派生クラスでオーバーライドされるとき、このインスタンスの値が派生クラスの既定値であるかどうかを示します。

(継承元 Attribute)
Match(Object)

派生クラス内でオーバーライドされたときに、指定したオブジェクトとこのインスタンスが等しいかどうかを示す値を返します。

(継承元 Attribute)
MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

明示的なインターフェイスの実装

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

一連の名前を対応する一連のディスパッチ識別子に割り当てます。

(継承元 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

オブジェクトの型情報を取得します。この情報はインターフェイスの型情報の取得に使用できます。

(継承元 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

オブジェクトが提供する型情報インターフェイスの数 (0 または 1) を取得します。

(継承元 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

オブジェクトによって公開されたプロパティおよびメソッドへのアクセスを提供します。

(継承元 Attribute)

適用対象

こちらもご覧ください