AppDomain.AssemblyResolve Událost
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Nastane, když řešení sestavení selže.
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 demonstruje AssemblyResolve událost.
Pro spuštění tohoto příkladu kódu musíte zadat plně kvalifikovaný název sestavení. Informace o získání plně kvalifikovaného názvu 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í určené vlastností ResolveEventArgs.Name nebo vrátit hodnotu null, pokud sestavení není rozpoznáno. Sestavení musí být načteno do kontextu spuštění; Pokud je načtena do kontextu jen pro reflexi, 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í načítání sestavení.
Počínaje rozhraním .NET Framework 4 ResolveEventArgs.RequestingAssembly vrátí vlastnost sestavení, které požadovalo načtení sestavení, které nebylo možné přeložit. Zavaděč například nemusí načíst závislost požadovaného sestavení, protože požadované sestavení a její závislost nejsou v cestě sondy. Znalost identity požadované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 verzí závislosti. Další informace najdete na webu ResolveEventArgs.RequestingAssembly.
Důležité
Počínaje rozhraním .NET Framework 4 ResolveEventHandler je událost vyvolána 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 se může volat 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 použití zásady.
Důležité
Pokud je pro tuto událost registrováno více obslužných rutin událostí, obslužné rutiny událostí se volají v pořadí, dokud obslužná rutina události nevrátí hodnotu, která není null. Další obslužné rutiny událostí se ignorují.
Další informace o zpracování událostí naleznete v tématu Zpracování a vyvolávání událostí.