AppDomain.AssemblyResolve Zdarzenie

Definicja

Występuje, gdy rozdzielczość zestawu kończy się niepowodzeniem.

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 

Typ zdarzenia

Implementuje

Atrybuty

Przykłady

Poniższy przykład pokazuje AssemblyResolve zdarzenie.

Aby ten przykładowy kod zadziałał, należy podać w pełni kwalifikowaną nazwę zestawu. Aby uzyskać informacje o sposobie uzyskania w pełni kwalifikowanej nazwy zestawu, zobacz Nazwy zestawów.

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

Uwagi

Jest to odpowiedzialność ResolveEventHandler za zwrócenie zestawu określonego ResolveEventArgs.Name przez właściwość lub zwrócenie wartości null, jeśli zestaw nie został rozpoznany. Zestaw musi zostać załadowany do kontekstu wykonywania; jeśli jest ładowany do kontekstu tylko odbicia, obciążenie, które spowodowało, że to zdarzenie zostało podniesione, kończy się niepowodzeniem.

Aby uzyskać wskazówki dotyczące korzystania z tego zdarzenia, zobacz Rozwiązywanie obciążeń zestawów.

Począwszy od .NET Framework 4, ResolveEventArgs.RequestingAssembly właściwość zwraca zestaw, który zażądał obciążenia zestawu, którego nie można rozpoznać. Na przykład moduł ładujący może nie być w stanie załadować zależności zestawu żądającego, ponieważ żądający zestaw i jego zależność nie znajdują się w ścieżce sondowania. Znajomość tożsamości żądanego zestawu może być przydatna podczas lokalizowania zależności lub identyfikowania poprawnej wersji, jeśli jest dostępna więcej niż jedna wersja zależności. Aby uzyskać więcej informacji, zobacz ResolveEventArgs.RequestingAssembly.

Ważne

Począwszy od .NET Framework 4, ResolveEventHandler zdarzenie jest wywoływane dla wszystkich zestawów, w tym zestawów zasobów. We wcześniejszych wersjach zdarzenie nie zostało zgłoszone dla zestawów zasobów. Jeśli system operacyjny jest zlokalizowany, program obsługi może być wywoływany wiele razy: raz dla każdej kultury w łańcuchu rezerwowym.

Dla tego zdarzenia ResolveEventArgs.Name właściwość zwraca nazwę zestawu przed zastosowaniem zasad.

Ważne

Jeśli dla tego zdarzenia zarejestrowano więcej niż jedną procedurę obsługi zdarzeń, programy obsługi zdarzeń są wywoływane w kolejności, dopóki program obsługi zdarzeń nie zwróci wartości, która nie nulljest . Kolejne programy obsługi zdarzeń są ignorowane.

Aby uzyskać więcej informacji na temat obsługi zdarzeń, zobacz Obsługa i podnoszenie zdarzeń.

Dotyczy

Zobacz też