ImportAttribute Classe

Definizione

Specifica che una proprietà, un campo o un valore di parametro deve essere fornito dall'oggetto CompositionContainer.

public ref class ImportAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property, AllowMultiple=false, Inherited=false)]
public class ImportAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property, AllowMultiple=false, Inherited=false)>]
type ImportAttribute = class
    inherit Attribute
Public Class ImportAttribute
Inherits Attribute
Ereditarietà
ImportAttribute
Attributi

Esempio

Nell'esempio seguente vengono illustrate tre classi con membri decorati con , ImportAttributee tre esportazioni corrispondenti.

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

Commenti

Nel modello di programmazione con attributi viene ImportAttribute usato per dichiarare le importazioni o le dipendenze di una determinata parte. Può decorare una proprietà, un campo o un metodo. Durante la composizione, le importazioni di una parte verranno riempite dall'oggetto CompositionContainer a cui appartiene tale parte usando le esportazioni fornite a tale CompositionContainer oggetto.

Se un'importazione corrisponde a una determinata esportazione viene determinata principalmente confrontando il nome del contratto e il tipo di contratto. In genere, non è necessario specificare uno di questi quando si usa l'attributo di importazione nel codice e verranno dedotti automaticamente dal tipo del membro decorato. Se l'importazione deve corrispondere a un'esportazione di un tipo diverso, ad esempio una sottoclasse del tipo del membro decorato o un'interfaccia implementata da tale membro, il tipo di contratto deve essere specificato in modo esplicito. Il nome del contratto può anche essere specificato in modo esplicito, ad esempio per distinguere tra più contratti con lo stesso tipo, ma in genere è preferibile eseguire questa operazione tramite metadati. Per altre informazioni sui metadati, vedere PartMetadataAttribute.

Costruttori

ImportAttribute()

Inizializza una nuova istanza della classe ImportAttribute, che imposta l'esportazione con il nome di contratto predefinito.

ImportAttribute(String)

Inizializza una nuova istanza della classe ImportAttribute, che importa l'esportazione con il nome di contratto specificato.

ImportAttribute(String, Type)

Inizializza una nuova istanza della classe ImportAttribute, che importa l'esportazione con il nome e il tipo di contratto specificati.

ImportAttribute(Type)

Inizializza una nuova istanza della classe ImportAttribute, che importa l'esportazione con il nome di contratto derivato dal tipo specificato.

Proprietà

AllowDefault

Ottiene o imposta un valore che indica se la proprietà, il campo o il parametro verrà impostato sul valore predefinito del relativo tipo quando un'esportazione con il nome del contratto non è presente nel contenitore.

AllowRecomposition

Ottiene o imposta un valore che indica se la proprietà o il campo sarà ricomposto quando le esportazioni con contratto corrispondente vengono modificate nel contenitore.

ContractName

Ottiene il nome del contratto dell'esportazione da importare.

ContractType

Ottiene il tipo dell'esportazione da importare.

RequiredCreationPolicy

Ottiene o imposta un valore che indica che l'utilità di importazione richiede un oggetto CreationPolicy specifico per le esportazioni utilizzate per soddisfare questa importazione.

Source

Ottiene o imposta un valore che specifica gli ambiti da cui questa importazione può essere soddisfatta.

TypeId

Quando è implementata in una classe derivata, ottiene un identificatore univoco della classe Attribute.

(Ereditato da Attribute)

Metodi

Equals(Object)

Restituisce un valore che indica se questa istanza è uguale a un oggetto specificato.

(Ereditato da Attribute)
GetHashCode()

Restituisce il codice hash per l'istanza.

(Ereditato da Attribute)
GetType()

Ottiene l'oggetto Type dell'istanza corrente.

(Ereditato da Object)
IsDefaultAttribute()

In caso di override in una classe derivata, indica se il valore di questa istanza è il valore predefinito per la classe derivata.

(Ereditato da Attribute)
Match(Object)

Quando è sottoposto a override in una classe derivata, restituisce un valore che indica se questa istanza equivale a un oggetto specificato.

(Ereditato da Attribute)
MemberwiseClone()

Crea una copia superficiale dell'oggetto Object corrente.

(Ereditato da Object)
ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.

(Ereditato da Object)

Implementazioni dell'interfaccia esplicita

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

Esegue il mapping di un set di nomi a un set corrispondente di ID dispatch.

(Ereditato da Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

Recupera le informazioni sul tipo relative a un oggetto, che possono essere usate per ottenere informazioni sul tipo relative a un'interfaccia.

(Ereditato da Attribute)
_Attribute.GetTypeInfoCount(UInt32)

Recupera il numero delle interfacce di informazioni sul tipo fornite da un oggetto (0 o 1).

(Ereditato da Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

Fornisce l'accesso a proprietà e metodi esposti da un oggetto.

(Ereditato da Attribute)

Si applica a

Vedi anche