AppDomain.AssemblyResolve Událost

Definice

Nastane při selhání řešení sestavení.

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

Event Type

Implementuje

Atributy

Příklady

Následující ukázka ukazuje AssemblyResolve událost.

Pro spuštění této ukázky kódu je nutné zadat plně kvalifikovaný název. Informace o tom, jak získat plně kvalifikovaný název sestavení, naleznete v tématu Názvy sestavení.

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 AssemblyResolveSnippet
{
    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;
    }
}
type MyType() =
    do
        printfn "\nMyType instantiated!"

let instantiateMyTypeFail (domain: 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")
        |> ignore
    with e ->
        printfn $"\n{e.Message}"

let instantiateMyTypeSucceed (domain: AppDomain) =
    try
        let asmname = Assembly.GetCallingAssembly().FullName
        domain.CreateInstance(asmname, "MyType")
        |> ignore
    with e ->
        printfn $"\n{e.Message}"

let myResolveEventHandler _ _ =
    printfn "Resolving..."
    typeof<MyType>.Assembly


let currentDomain = AppDomain.CurrentDomain

// This call will fail to create an instance of MyType since the
// assembly resolver is not set
instantiateMyTypeFail currentDomain

currentDomain.add_AssemblyResolve (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
Public Class MyType

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

End Class

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

    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

    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

Poznámky

Je odpovědností ResolveEventHandler za tuto událost vrátit sestavení, které je určeno ResolveEventArgs.Name vlastností, nebo vrátit null, pokud sestavení není rozpoznáno. Sestavení musí být načteno do kontextu provádění; pokud se načte do kontextu jen reflexe, zatížení, které způsobilo vyvolání této události, selže.

Pokyny k použití této události najdete v tématu Řešení zatížení sestavení.

Počínaje rozhraním .NET Framework 4 vrátí vlastnost sestavení, ResolveEventArgs.RequestingAssembly které požadovalo zatížení sestavení, které nelze vyřešit. Zavaděč například nemusí být schopen načíst závislost žádajícího sestavení, protože žádající sestavení a jeho závislost nejsou ve zkoumací cestě. Znalost identity žádajícího sestavení může být užitečná při vyhledání závislosti nebo při identifikaci správné verze, pokud je k dispozici více než jedna verze závislosti. Další informace naleznete v tématu ResolveEventArgs.RequestingAssembly.

Důležité

Počínaje rozhraním .NET Framework 4 ResolveEventHandler se událost vyvolá pro všechna sestavení, včetně sestavení prostředků. V dřívějších verzích nebyla událost vyvolána pro sestavení prostředků. Pokud je operační systém lokalizován, obslužná rutina může být volána vícekrát: jednou pro každou jazykovou verzi v záložním řetězci.

Pro tuto událost ResolveEventArgs.Name vrátí vlastnost název sestavení před uplatněním zásady.

Důležité

Pokud je pro tuto událost zaregistrováno více než jedna obslužná rutina události, jsou obslužné rutiny událostí volána v pořadí, dokud obslužná rutina události nevrátí hodnotu, která není null. Následné obslužné rutiny událostí jsou ignorovány.

Další informace o zpracování událostí najdete v tématu Zpracování a vyvolávání událostí.

Platí pro

Viz také