英語で読む

次の方法で共有


ModuleResolveEventHandler 代理人

定義

AssemblyModuleResolve イベントを処理するメソッドを表します。

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

パラメーター

sender
Object

イベントのソースであるアセンブリ。

e
ResolveEventArgs

イベントを記述しているオブジェクトによって提供されている引数。

戻り値

要求を満たすモジュール。

属性

次の例では、イベント ハンドラーが呼び出される一連の実行を示します。 この例では、Server1 は MySample クラスの外部モジュールです。

C#
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();
    }
}
C#
// 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