ExportAttribute Osztály
Definíció
Fontos
Egyes információk olyan, kiadás előtti termékekre vonatkoznak, amelyek a kiadásig még jelentősen módosulhatnak. A Microsoft nem vállal kifejezett vagy törvényi garanciát az itt megjelenő információért.
Megadja, hogy egy típus, tulajdonság, mező vagy metódus adott exportálást biztosít-e.
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
- Öröklődés
- Attribútumok
Példák
Az alábbi példa azt mutatja be, hogy az ExportAttribute három osztályra van alkalmazva, és három, azokkal egyező importálásra.
//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
Megjegyzések
Az attribútumos programozási modellben az ExportAttribute attribútum deklarálja, hogy egy rész exportál vagy biztosít a kompozíciós tárolónak egy adott szerződést teljesítő objektumot. Az összeállítás során az egyező szerződéssel rendelkező importrészeket az exportált objektum tölti ki.
Az ExportAttribute attribútum egy teljes osztályra, illetve egy osztály tulajdonságára, mezőjére vagy metódusára is alkalmazható. Ha az egész osztályra van alkalmazva, az osztály egy példánya az exportált objektum. Ha egy osztály egy tagjára alkalmazza, az exportált objektum az adott tag értéke lesz.
Azt, hogy egy szerződés egyezését elsősorban a szerződés neve és a szerződés típusa határozza-e meg. További információkért lásd a ImportAttribute hivatkozást.
Konstruktorok
| Name | Description |
|---|---|
| ExportAttribute() |
Inicializálja az ExportAttribute osztály új példányát, és exportálja az ezzel az attribútummal megjelölt típust vagy tagot az alapértelmezett szerződésnév alatt. |
| ExportAttribute(String, Type) |
Inicializálja az ExportAttribute osztály új példányát, és exportálja a megadott típust a megadott szerződésnév alatt. |
| ExportAttribute(String) |
Inicializálja az ExportAttribute osztály új példányát, és exportálja az attribútummal megjelölt típust vagy tagot a megadott szerződésnév alatt. |
| ExportAttribute(Type) |
Inicializálja az ExportAttribute osztály új példányát, és exportálja az attribútummal megjelölt típust vagy tagot a megadott típusból származtatott szerződésnév alatt. |
Tulajdonságok
| Name | Description |
|---|---|
| ContractName |
Lekéri az attribútummal megjelölt típus vagy tag exportálásához használt szerződésnevet. |
| ContractType |
Lekéri az attribútum által megjelölt tag által exportált szerződéstípust. |
Metódusok
| Name | Description |
|---|---|
| Equals(Object) |
Olyan értéket ad vissza, amely jelzi, hogy ez a példány egyenlő-e egy adott objektummal. (Öröklődés forrása Attribute) |
| GetHashCode() |
A példány kivonatkódját adja vissza. (Öröklődés forrása Attribute) |
| GetType() |
Lekéri az Type aktuális példányt. (Öröklődés forrása Object) |
| MemberwiseClone() |
Az aktuális Objectpéldány sekély másolatát hozza létre. (Öröklődés forrása Object) |
| ToString() |
Az aktuális objektumot jelképező sztringet ad vissza. (Öröklődés forrása Object) |