AppDomain.AssemblyResolve 事件
在对程序集的解析失败时发生。
**命名空间:**System
**程序集:**mscorlib(在 mscorlib.dll 中)
语法
声明
Public Event AssemblyResolve As ResolveEventHandler
用法
Dim instance As AppDomain
Dim handler As ResolveEventHandler
AddHandler instance.AssemblyResolve, handler
public event ResolveEventHandler AssemblyResolve
public:
virtual event ResolveEventHandler^ AssemblyResolve {
void add (ResolveEventHandler^ value) sealed;
void remove (ResolveEventHandler^ value) sealed;
}
/** @event */
public final void add_AssemblyResolve (ResolveEventHandler value)
/** @event */
public final void remove_AssemblyResolve (ResolveEventHandler value)
JScript 支持使用事件,但不支持进行新的声明。
备注
此事件的 ResolveEventHandler 负责返回解析类型、程序集或资源的程序集。
若要为此事件注册事件处理程序,必须具有所需权限,否则将引发 SecurityException。
有关处理事件的更多信息,请参见 使用事件。
示例
下面的示例阐释 AssemblyResolve 事件。
要运行该代码实例,必须提供完全限定程序集名称。有关如何获取完全限定程序集名称的信息,请参见 程序集名称。
Public Class MyType
Public Sub New()
Console.WriteLine("MyType instantiated!")
End Sub 'New
End Class 'MyType
Module Test
Sub Main()
Dim currentDomain As AppDomain = AppDomain.CurrentDomain
InstantiateMyType(currentDomain) ' Failed!
AddHandler currentDomain.AssemblyResolve, AddressOf MyResolveEventHandler
InstantiateMyType(currentDomain) ' OK!
End Sub 'Main
Sub InstantiateMyType(domain As AppDomain)
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(e.Message)
End Try
End Sub 'InstantiateMyType
Function MyResolveEventHandler(sender As Object, args As ResolveEventArgs) As [Assembly]
Console.WriteLine("Resolving...")
Return GetType(MyType).Assembly
End Function 'MyResolveEventHandler
End Module 'Test
public class MyType {
public MyType() {
Console.WriteLine("MyType instantiated!");
}
}
class Test {
public static void Main() {
AppDomain currentDomain = AppDomain.CurrentDomain;
InstantiateMyType(currentDomain); // Failed!
currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);
InstantiateMyType(currentDomain); // OK!
}
static void InstantiateMyType(AppDomain domain) {
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(e.Message);
}
}
static Assembly MyResolveEventHandler(object sender, ResolveEventArgs args) {
Console.WriteLine("Resolving...");
return typeof(MyType).Assembly;
}
}
public ref class MyType
{
public:
MyType()
{
Console::WriteLine( "MyType instantiated!" );
}
};
ref class Test
{
public:
static void InstantiateMyType( AppDomain^ domain )
{
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( e->Message );
}
}
static Assembly^ MyResolveEventHandler( Object^ sender, ResolveEventArgs^ args )
{
Console::WriteLine( "Resolving..." );
return MyType::typeid->Assembly;
}
};
int main()
{
AppDomain^ currentDomain = AppDomain::CurrentDomain;
Test::InstantiateMyType( currentDomain ); // Failed!
currentDomain->AssemblyResolve += gcnew ResolveEventHandler( Test::MyResolveEventHandler );
Test::InstantiateMyType( currentDomain ); // OK!
}
.NET Framework 安全性
- SecurityPermission 用于为此事件添加事件处理程序。关联的枚举:SecurityPermissionFlag.ControlAppDomain。
平台
Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0