ModuleResolveEventHandler 委托

定义

表示将要处理 AssemblyModuleResolve 事件的方法。

public delegate System.Reflection.Module ModuleResolveEventHandler(object sender, ResolveEventArgs e);
[System.Serializable]
public delegate System.Reflection.Module ModuleResolveEventHandler(object sender, ResolveEventArgs e);
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public delegate System.Reflection.Module ModuleResolveEventHandler(object sender, ResolveEventArgs e);

参数

sender
Object

曾作为事件源的程序集。

e
ResolveEventArgs

由描述事件的对象提供的参数。

返回值

满足请求的模块。

属性

示例

以下示例演示调用事件处理程序的执行序列。 在此示例中,Server1 是 MySample 类的外部模块。

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;
    }
}

编译并运行示例:

  1. 使用以下命令编译 Server1:

    csc /out:subfolder\Server1.netmodule /t:module Server1.cs
    
  2. 使用以下命令编译 MySample:

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

备注

模块文件 Server1.netmodule 必须位于名为“subfolder”的子目录中,此示例才能正常工作。

注解

如果公共语言运行时类加载程序无法通过正常方式解析对程序集内部模块的引用,则会引发此事件。 此委托的事件处理程序必须查找并返回满足请求的模块。

扩展方法

GetMethodInfo(Delegate)

获取指示指定委托表示的方法的对象。

适用于

产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1