AppDomain.AssemblyResolve AppDomain.AssemblyResolve AppDomain.AssemblyResolve AppDomain.AssemblyResolve Event

Definizione

Si verifica quando la risoluzione di un assembly ha esito negativo.Occurs when the resolution of an assembly fails.

public:
 virtual event ResolveEventHandler ^ AssemblyResolve;
[add: System.Security.SecurityCritical]
[remove: System.Security.SecurityCritical]
public event ResolveEventHandler AssemblyResolve;
member this.AssemblyResolve : ResolveEventHandler 
Public Custom Event AssemblyResolve As ResolveEventHandler 

Implementazioni

Attributi

Esempi

Nell'esempio seguente viene illustrato il AssemblyResolve evento.The following sample demonstrates the AssemblyResolve event.

Per questo esempio di codice per l'esecuzione, è necessario specificare il nome completo dell'assembly.For this code example to run, you must provide the fully qualified assembly name. Per informazioni su come ottenere il nome completo dell'assembly, vedere i nomi degli Assembly.For information about how to obtain the fully qualified assembly name, see Assembly Names.

public ref class MyType
{
public:
    MyType()
    {
        Console::WriteLine();
        Console::WriteLine("MyType instantiated!");
    }
};

class Test
{
public:
    static void Main()
    {
        AppDomain^ currentDomain = AppDomain::CurrentDomain;

        // This call will fail to create an instance of MyType since the
        // assembly resolver is not set
        InstantiateMyTypeFail(currentDomain);

        currentDomain->AssemblyResolve += gcnew ResolveEventHandler(&Test::MyResolveEventHandler);

        // This call will succeed in creating an instance of MyType since the
        // assembly resolver is now set.
        InstantiateMyTypeFail(currentDomain);

        // This call will succeed in creating an instance of MyType since the
        // assembly name is valid.
        InstantiateMyTypeSucceed(currentDomain);
    }

private:
    static void InstantiateMyTypeFail(AppDomain^ domain)
    {
        // Calling InstantiateMyType will always fail since the assembly info
        // given to CreateInstance is invalid.
        try
        {
            // You must supply a valid fully qualified assembly name here.
            domain->CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyType");
        }
        catch (Exception^ e)
        {
            Console::WriteLine();
            Console::WriteLine(e->Message);
        }
    }

    static void InstantiateMyTypeSucceed(AppDomain^ domain)
    {
        try
        {
            String^ asmname = Assembly::GetCallingAssembly()->FullName;
            domain->CreateInstance(asmname, "MyType");
        }
        catch (Exception^ e)
        {
            Console::WriteLine();
            Console::WriteLine(e->Message);
        }
    }

    static Assembly^ MyResolveEventHandler(Object^ sender, ResolveEventArgs^ args)
    {
        Console::WriteLine("Resolving...");
        return MyType::typeid->Assembly;
    }
};

int main()
{
    Test::Main();
}
public class MyType
{
    public MyType()
    {
        Console.WriteLine();
        Console.WriteLine("MyType instantiated!");
    }
}

class Test
{
    public static void Main()
    {
        AppDomain currentDomain = AppDomain.CurrentDomain;

        // This call will fail to create an instance of MyType since the
        // assembly resolver is not set
        InstantiateMyTypeFail(currentDomain);

        currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);

        // This call will succeed in creating an instance of MyType since the
        // assembly resolver is now set.
        InstantiateMyTypeFail(currentDomain);

        // This call will succeed in creating an instance of MyType since the
        // assembly name is valid.
        InstantiateMyTypeSucceed(currentDomain);
    }

    private static void InstantiateMyTypeFail(AppDomain domain)
    {
        // Calling InstantiateMyType will always fail since the assembly info
        // given to CreateInstance is invalid.
        try
        {
            // You must supply a valid fully qualified assembly name here.
            domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyType");
        }
        catch (Exception e)
        {
            Console.WriteLine();
            Console.WriteLine(e.Message);
        }
    }

    private static void InstantiateMyTypeSucceed(AppDomain domain)
    {
        try
        {
            string asmname = Assembly.GetCallingAssembly().FullName;
            domain.CreateInstance(asmname, "MyType");
        }
        catch (Exception e)
        {
            Console.WriteLine();
            Console.WriteLine(e.Message);
        }
    }

    private static Assembly MyResolveEventHandler(object sender, ResolveEventArgs args)
    {
        Console.WriteLine("Resolving...");
        return typeof(MyType).Assembly;
    }
}
Public Class MyType

    Public Sub New()
        Console.WriteLine()
        Console.WriteLine("MyType instantiated!")
    End Sub 'New

End Class 'MyType

Class Test

    Public Shared Sub Main()
        Dim currentDomain As AppDomain = AppDomain.CurrentDomain

        ' This call will fail to create an instance of MyType since the
        ' assembly resolver is not set
        InstantiateMyTypeFail(currentDomain)

        AddHandler currentDomain.AssemblyResolve, AddressOf MyResolveEventHandler

        ' This call will succeed in creating an instance of MyType since the
        ' assembly resolver is now set.
        InstantiateMyTypeFail(currentDomain)

        ' This call will succeed in creating an instance of MyType since the
        ' assembly name is valid.
        InstantiateMyTypeSucceed(currentDomain)
    End Sub 'Main

    Private Shared Sub InstantiateMyTypeFail(domain As AppDomain)
        ' Calling InstantiateMyType will always fail since the assembly info
        ' given to CreateInstance is invalid.
        Try
            ' You must supply a valid fully qualified assembly name here.
            domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyType")
        Catch e As Exception
            Console.WriteLine()
            Console.WriteLine(e.Message)
        End Try
    End Sub 'InstantiateMyType

    Private Shared Sub InstantiateMyTypeSucceed(domain As AppDomain)
        Try
            Dim asmname As String = Assembly.GetCallingAssembly().FullName
            domain.CreateInstance(asmname, "MyType")
        Catch e As Exception
            Console.WriteLine()
            Console.WriteLine(e.Message)
        End Try
    End Sub

    Private Shared Function MyResolveEventHandler(sender As Object, args As ResolveEventArgs) As Assembly
        Console.WriteLine("Resolving...")
        Return GetType(MyType).Assembly
    End Function 'MyResolveEventHandler

End Class 'Test

Commenti

È responsabilità del ResolveEventHandler per questo evento restituire l'assembly specificato da di ResolveEventArgs.Name proprietà o per restituire null se l'assembly non viene riconosciuto.It is the responsibility of the ResolveEventHandler for this event to return the assembly that is specified by the ResolveEventArgs.Name property, or to return null if the assembly is not recognized. L'assembly deve essere caricato in un contesto di esecuzione; Se viene caricato nel contesto reflection-only, il carico che ha causato la generazione dell'evento ha esito negativo.The assembly must be loaded into an execution context; if it is loaded into the reflection-only context, the load that caused this event to be raised fails.

Per indicazioni sull'uso di questo evento, vedere Risoluzione caricamenti Assembly.For guidance on the use of this event, see Resolving Assembly Loads.

Inizia con la .NET Framework 4.NET Framework 4, il ResolveEventArgs.RequestingAssembly proprietà restituisce l'assembly che ha richiesto il caricamento dell'assembly che non può essere risolto.Beginning with the .NET Framework 4.NET Framework 4, the ResolveEventArgs.RequestingAssembly property returns the assembly that requested the assembly load that could not be resolved. Ad esempio, il caricatore potrebbe essere Impossibile caricare una dipendenza di assembly richiedente, perché l'assembly richiedente e la relativa dipendenza non sono nel percorso di sondaggio.For example, the loader might be unable to load a dependency of the requesting assembly because the requesting assembly and its dependency are not in the probing path. Conoscere l'identità dell'assembly richiesta potrebbe essere utile nell'individuazione della dipendenza o identificare la versione corretta, se è disponibile più di una versione della dipendenza.Knowing the identity of the requesting assembly might be useful in locating the dependency or in identifying the correct version, if more than one version of the dependency is available. Per altre informazioni, vedere ResolveEventArgs.RequestingAssembly.For more information, see ResolveEventArgs.RequestingAssembly.

Importante

Inizia con la .NET Framework 4.NET Framework 4, il ResolveEventHandler evento viene generato per tutti gli assembly, inclusi gli assembly di risorse.Beginning with the .NET Framework 4.NET Framework 4, the ResolveEventHandler event is raised for all assemblies, including resource assemblies. Nelle versioni precedenti, per gli assembly di risorse non è stato generato l'evento.In earlier versions, the event was not raised for resource assemblies. Se il sistema operativo localizzato, il gestore può essere chiamato più volte: una volta per ogni impostazione cultura nella catena di fallback.If the operating system is localized, the handler might be called multiple times: once for each culture in the fallback chain.

Per questo evento, il ResolveEventArgs.Name proprietà restituisce il nome dell'assembly prima di applicare dei criteri.For this event, the ResolveEventArgs.Name property returns the assembly name before policy is applied.

Importante

Se più di un gestore eventi è registrato per questo evento, i gestori eventi vengono chiamati in ordine fino a quando un gestore eventi restituisce un valore che non sia null.If more than one event handler is registered for this event, the event handlers are called in order until an event handler returns a value that isn't null. Gestori di eventi successivi vengono ignorati.Subsequent event handlers are ignored.

Per ulteriori informazioni sulla gestione degli eventi, consultare gestione e generazione di eventi.For more information about handling events, see Handling and Raising Events.

Sicurezza

SecurityCriticalAttribute
richiede l'attendibilità totale per il chiamante immediato.Requires full trust for the immediate caller. Questo membro non può essere utilizzato da codice parzialmente attendibile o trasparente.This member cannot be used by partially trusted or transparent code.

Si applica a

Vedi anche