ModuleResolveEventHandler 委托

定义

表示将要处理 AssemblyModuleResolve 事件的方法。Represents the method that will handle the ModuleResolve event of an Assembly.

public delegate System::Reflection::Module ^ ModuleResolveEventHandler(System::Object ^ sender, ResolveEventArgs ^ e);
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public delegate System.Reflection.Module ModuleResolveEventHandler(object sender, ResolveEventArgs e);
type ModuleResolveEventHandler = delegate of obj * ResolveEventArgs -> Module
Public Delegate Function ModuleResolveEventHandler(sender As Object, e As ResolveEventArgs) As Module 

参数

sender
Object

曾作为事件源的程序集。The assembly that was the source of the event.

e
ResolveEventArgs

由描述事件的对象提供的参数。The arguments supplied by the object describing the event.

返回值

Module

满足请求的模块。The module that satisfies the request.

继承
ModuleResolveEventHandler
属性

示例

下面的示例演示调用事件处理程序的执行顺序。The following example demonstrates the sequence of execution in which an event handler is called. 在此示例中, Server1 是 MySample 类的外部模块。In this example, Server1 is an external module of the MySample class.

编译并运行示例To compile and run the example

  1. 打开命令提示符窗口并导航到包含 MySample.cs 的目录。Open a Command Prompt window and navigate to the directory that contains MySample.cs.

  2. 使用以下命令编译 Server1:Compile Server1 using the following command:

    csc /out:subfolder\Server1.netmodule /t:module Server1.cs  
    
  3. 使用以下命令编译 MySample:Compile MySample using the following command:

    csc /out:MySample.exe /t:exe /addmodule:subfolder\Server1.netmodule MySample.cs  
    
  4. 运行 MySample。Run MySample.exe.

备注

模块文件 .netmodule 必须位于名为 "子文件夹" 的子目录中, 此示例才能正常运行。The module file Server1.netmodule must be in a subdirectory named "subfolder" for this example to work properly.

using System;
using System.IO;
using System.Reflection;

class MySample
{
    public static int Main(String[] args)
    {
        Assembly asm1 = typeof(MySample).Assembly;
        asm1.ModuleResolve += new ModuleResolveEventHandler(evModuleResolve);
        Console.WriteLine("Calling MySample.Test...");
        Test();
        return 0;
    }
    private static Module evModuleResolve(object sender, ResolveEventArgs e)
    {
        Console.WriteLine();
        Console.WriteLine("******************************************************");
        Console.WriteLine("* MySample.evModuleResolve() in module: {0:s} *",
            Type.GetType("MySample").Module.ScopeName);
        Console.WriteLine("******************************************************");
        FileStream fs = File.Open("subfolder\\Server1.netmodule", FileMode.Open);
        long len = fs.Length;
        byte[] rgFileBytes = new byte[len];
        fs.Read(rgFileBytes, 0, (int)len);
        Assembly a = typeof(MySample).Assembly;
        Module m = a.LoadModule("Server1.netmodule", rgFileBytes);
        return m;
    }
    private static void Test()
    {
        Console.WriteLine("Instantiating Server1...");
        Server1 s = new Server1();
        Console.WriteLine("Calling Server1.trivial...");
        s.trivial();
    }
}
// Server1 module

using System;
using System.Reflection;

public class Server1 : MarshalByRefObject
{
    public int trivial()
    {
        Console.WriteLine();
        Console.WriteLine("******************************************************");
        Console.WriteLine("*   Server1.trivial() in module: {0:s}   *", this.GetType().Module.ScopeName);
        Console.WriteLine("******************************************************");
        Console.WriteLine("Returning from Server1.trivial...");
        return 1;
    }
}

注解

如果公共语言运行时类加载程序不能通过正常方法解析对程序集的内部模块的引用, 则会引发此事件。If the common language runtime class loader cannot resolve a reference to an internal module of an assembly through normal means, this event is raised. 此委托的事件处理程序必须找到并返回满足请求的模块。The event handler for this delegate must locate and return a module that satisfies the request.

扩展方法

GetMethodInfo(Delegate)

获取指示指定委托表示的方法的对象。Gets an object that represents the method represented by the specified delegate.

适用于