ModuleResolveEventHandler ModuleResolveEventHandler ModuleResolveEventHandler ModuleResolveEventHandler Delegate

定義

代表將處理 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 Object Object Object

事件來源的組件。The assembly that was the source of the event.

e
ResolveEventArgs ResolveEventArgs ResolveEventArgs ResolveEventArgs

描述事件的物件所提供的引數。The arguments supplied by the object describing the event.

傳回值

System.Reflection.Module

滿足此要求的模組。The module that satisfies the request.

繼承
ModuleResolveEventHandlerModuleResolveEventHandlerModuleResolveEventHandlerModuleResolveEventHandler
屬性

範例

下列範例示範呼叫事件處理常式的執行順序。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.

注意

模組檔案 Server1. .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;
    }
}

備註

如果 common language runtime 類別載入器無法透過正常方式解析元件內部模組的參考, 就會引發這個事件。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) GetMethodInfo(Delegate) GetMethodInfo(Delegate) GetMethodInfo(Delegate)

取得表示特定委派所代表之方法的物件。Gets an object that represents the method represented by the specified delegate.

適用於