ExportAttribute 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
指定類型、屬性、欄位或方法提供特定的匯出。
public ref class ExportAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Property, AllowMultiple=true, Inherited=false)]
public class ExportAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Property, AllowMultiple=true, Inherited=false)>]
type ExportAttribute = class
inherit Attribute
Public Class ExportAttribute
Inherits Attribute
- 繼承
- 屬性
範例
以下範例顯示 ExportAttribute 被套用到三個類別,以及三個符合它們的匯入。
//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 cases. 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 cases. 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 參考。
建構函式
| 名稱 | Description |
|---|---|
| ExportAttribute() |
初始化該類別的新實例 ExportAttribute ,並將預設合約名稱下標記為此屬性的類型或成員匯出。 |
| ExportAttribute(String, Type) |
初始化該類別的新實例 ExportAttribute ,並以指定的合約名稱匯出指定的型別。 |
| ExportAttribute(String) |
初始化該類別的新實例 ExportAttribute ,匯出以指定合約名稱標示此屬性的類型或成員。 |
| ExportAttribute(Type) |
初始化該類別的新實例 ExportAttribute ,並以指定類型衍生的合約名稱匯出帶有此屬性的類型或成員。 |
屬性
| 名稱 | Description |
|---|---|
| ContractName |
取得用來匯出帶有此屬性的類型或成員的合約名稱。 |
| ContractType |
取得由該屬性標記的成員匯出的合約類型。 |
方法
| 名稱 | Description |
|---|---|
| Equals(Object) |
傳回值,這個值表示這個實例是否等於指定的物件。 (繼承來源 Attribute) |
| GetHashCode() |
傳回這個實例的哈希碼。 (繼承來源 Attribute) |
| GetType() |
取得目前實例的 Type。 (繼承來源 Object) |
| MemberwiseClone() |
建立目前 Object的淺層複本。 (繼承來源 Object) |
| ToString() |
傳回表示目前 物件的字串。 (繼承來源 Object) |