Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
ModuleResolveEventHandler Delegate
Collapse the table of content
Expand the table of content

ModuleResolveEventHandler Delegate

Represents the method that will handle the ModuleResolve event of an Assembly.

Namespace:  System.Reflection
Assembly:  mscorlib (in mscorlib.dll)

[SerializableAttribute]
[ComVisibleAttribute(true)]
public delegate Module ModuleResolveEventHandler(
	Object sender,
	ResolveEventArgs e
)

Parameters

sender
Type: System.Object

The assembly that was the source of the event.

e
Type: System.ResolveEventArgs

The arguments supplied by the object describing the event.

Return Value

Type: System.Reflection.Module
The module that satisfies the request.

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.

The following example demonstrates the sequence of execution in which an event handler is called. In this example, Server1 is an external module of the MySample class.

To compile and run the example

  1. Open a Command Prompt window and navigate to the directory that contains MySample.cs.

  2. Compile Server1 using the following command:

    csc /out:subfolder\Server1.netmodule /t:module Server1.cs
    
  3. Compile MySample using the following command:

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

NoteNote

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

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Show:
© 2015 Microsoft