ExportAttribute ExportAttribute ExportAttribute ExportAttribute Class

Definición

Especifica que un tipo, una propiedad, un campo o un método proporciona una exportación determinada.Specifies that a type, property, field, or method provides a particular export.

public ref class ExportAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Field | System.AttributeTargets.Method | System.AttributeTargets.Property, AllowMultiple=true, Inherited=false)]
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Method | System.AttributeTargets.Property | System.AttributeTargets.Field, AllowMultiple=true, Inherited=false)]
public class ExportAttribute : Attribute
type ExportAttribute = class
    inherit Attribute
Public Class ExportAttribute
Inherits Attribute
Herencia
ExportAttributeExportAttributeExportAttributeExportAttribute
Derivado
Atributos

Ejemplos

El ejemplo siguiente muestra tres clases decoradas con el ExportAttributey tres importaciones que coincidan con ellos.The following example shows three classes decorated with the ExportAttribute, and three imports that match them.

//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

Comentarios

En el modelo de programación con atributos, el ExportAttribute declara que una parte exporta, o proporciona al contenedor de composición, un objeto que cumple un contrato determinado.In the Attributed Programming Model, the ExportAttribute declares that a part exports, or provides to the composition container, an object that fulfills a particular contract. Durante la composición, las partes con importaciones que tienen contratos coincidentes tendrán esas dependencias rellenadas por el objeto exportado.During composition, parts with imports that have matching contracts will have those dependencies filled by the exported object.

El ExportAttribute puede decorar una clase entera o una propiedad, campo o método de una clase.The ExportAttribute can decorate either an entire class, or a property, field, or method of a class. Si está decorada toda la clase, una instancia de la clase es el objeto exportado.If the entire class is decorated, an instance of the class is the exported object. Si un miembro de una clase está decorado, el objeto exportado será el valor del miembro representativo.If a member of a class is decorated, the exported object will be the value of the decorated member.

O no coincide con un contrato viene determinada principalmente por el nombre de contrato y el tipo de contrato.Whether or not a contract matches is determined primarily by the contract name and the contract type. Para obtener más información, vea ImportAttribute.For more information, see ImportAttribute.

Constructores

ExportAttribute() ExportAttribute() ExportAttribute() ExportAttribute()

Inicializa una nueva instancia de la clase ExportAttribute, exportando el tipo o el miembro marcado con este atributo bajo el nombre de contrato predeterminado.Initializes a new instance of the ExportAttribute class, exporting the type or member marked with this attribute under the default contract name.

ExportAttribute(String) ExportAttribute(String) ExportAttribute(String) ExportAttribute(String)

Inicializa una nueva instancia de la clase ExportAttribute y exporta el tipo o el miembro marcado con este atributo bajo el nombre de contrato especificado.Initializes a new instance of the ExportAttribute class, exporting the type or member marked with this attribute under the specified contract name.

ExportAttribute(String, Type) ExportAttribute(String, Type) ExportAttribute(String, Type) ExportAttribute(String, Type)

Inicializa una nueva instancia de la clase ExportAttribute, exportando el tipo indicado con el nombre de contrato especificado.Initializes a new instance of the ExportAttribute class, exporting the specified type under the specified contract name.

ExportAttribute(Type) ExportAttribute(Type) ExportAttribute(Type) ExportAttribute(Type)

Inicializa una nueva instancia de la clase ExportAttribute, exportando el tipo o el miembro marcado con este atributo bajo un nombre de contrato derivado del tipo especificado.Initializes a new instance of the ExportAttribute class, exporting the type or member marked with this attribute under a contract name derived from the specified type.

Propiedades

ContractName ContractName ContractName ContractName

Obtiene el nombre de contrato que se usa para exportar el tipo o el miembro marcado con este atributo.Gets the contract name that is used to export the type or member marked with this attribute.

ContractType ContractType ContractType ContractType

Obtiene el tipo de contrato exportado por el miembro al que está adjunto este atributo.Gets the contract type that is exported by the member that this attribute is attached to.

TypeId TypeId TypeId TypeId

Cuando se implementa en una clase derivada, obtiene un identificador único para este Attribute.When implemented in a derived class, gets a unique identifier for this Attribute.

(Inherited from Attribute)

Métodos

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

Devuelve un valor que indica si esta instancia es igual que un objeto especificado.Returns a value that indicates whether this instance is equal to a specified object.

(Inherited from Attribute)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Devuelve el código hash de esta instancia.Returns the hash code for this instance.

(Inherited from Attribute)
GetType() GetType() GetType() GetType()

Obtiene el Type de la instancia actual.Gets the Type of the current instance.

(Inherited from Object)
IsDefaultAttribute() IsDefaultAttribute() IsDefaultAttribute() IsDefaultAttribute()

Si se reemplaza en una clase derivada, indica si el valor de esta instancia es el valor predeterminado de la clase derivada.When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(Inherited from Attribute)
Match(Object) Match(Object) Match(Object) Match(Object)

Cuando se invalida en una clase derivada, devuelve un valor que indica si esta instancia es igual a un objeto especificado.When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(Inherited from Attribute)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Crea una copia superficial del objeto Object actual.Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

Devuelve una cadena que representa el objeto actual.Returns a string that represents the current object.

(Inherited from Object)

Implementaciones de interfaz explícitas

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

Asigna un conjunto de nombres a un conjunto correspondiente de identificadores de envío.Maps a set of names to a corresponding set of dispatch identifiers.

(Inherited from Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

Obtiene la información de tipos de un objeto, que puede utilizarse para obtener la información de tipos de una interfaz.Retrieves the type information for an object, which can be used to get the type information for an interface.

(Inherited from Attribute)
_Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32)

Recupera el número de interfaces de información de tipo que proporciona un objeto (0 ó 1).Retrieves the number of type information interfaces that an object provides (either 0 or 1).

(Inherited from Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

Proporciona acceso a las propiedades y los métodos expuestos por un objeto.Provides access to properties and methods exposed by an object.

(Inherited from Attribute)

Se aplica a

Consulte también: