Export (0) Print
Expand All
Expand Minimize
0 out of 1 rated this helpful - Rate this topic

ModuleResolveEventHandler Delegate

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

[Visual Basic]
<Serializable>
Public Delegate Function Sub ModuleResolveEventHandler( _
   ByVal sender As Object, _
   ByVal e As ResolveEventArgs _
) As Module
[C#]
[Serializable]
public delegate Module ModuleResolveEventHandler(
   object sender,
   ResolveEventArgs e
);
[C++]
[Serializable]
public __gc __delegate Module* ModuleResolveEventHandler(
   Object* sender,
   ResolveEventArgs* e
);

[JScript] In JScript, you can use the delegates in the .NET Framework, but you cannot define your own.

Parameters [Visual Basic, C#, C++]

The declaration of your event handler must have the same parameters as the ModuleResolveEventHandler delegate declaration.

sender
The assembly that was the source of the event.
e
The arguments supplied by the object describing the event.

Remarks

When you create an Assembly delegate, you identify the method that will handle the event. To associate the event with your event handler, add an instance of the delegate to the event. The event handler is called whenever the event occurs, unless you remove the delegate. For more information about event handler delegates, see Events and Delegates.

If the common language runtime class loader cannot resolve a reference to an internal module of an assembly through normal means, the event is raised to give the callback a chance to find or load the module itself and return it.

Each derived class of Delegate and MulticastDelegate has a constructor and an Invoke method. See the Managed Extensions for C++ code example given in the description for the Delegate.

Example

[C#] 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.

[C#] Compile and run this example as follows.

  1. Compile Server1.
  2. Compile MySample.
  3. Run MySample. Note that the module file Server1.netmodule must be in a subfolder named "subfolder" for this to work.

[C#] Compile Server1 as follows:

[C#] csc

/out:subfolder\Server1.netmodule /t:module Server1.cs

[C#] 
using System;
using System.Reflection;
public class Server1 : MarshalByRefObject 
{
    public int trivial()
    {
        Console.WriteLine ("server1.trivial");
        return 1;
    }
}

[C#] Compile MySample as follows:

[C#] csc /out:MySample.exe /t:exe

/addmodule:subfolder\Server1.netmodule MySample.cs

[C#] 
using System;
using System.IO;
using System.Reflection;
class MySample
{
    public static int Main(String[] args)
    {
        Assembly asm1 = Assembly.GetExecutingAssembly();
        asm1.ModuleResolve += new ModuleResolveEventHandler (evModuleResolve);
        Console.WriteLine("Calling MySample.Test");
        Test();
        return 0;
    }
    private static Module evModuleResolve(object sender, ResolveEventArgs e)
    {
        Console.WriteLine("MySample.evModuleResolve");
        FileStream fs = File.Open(".\\subfolder\\server1.netmodule", FileMode.Open);
        byte [] rgFileBytes = new byte [1];
        long len = fs.Length;
        rgFileBytes = new byte[len];
        fs.Read(rgFileBytes, 0, (int)len);
        Assembly a = Assembly.GetExecutingAssembly();
        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();
    }    
}

[Visual Basic, C++, JScript] No example is available for Visual Basic, C++, or JScript. To view a C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Reflection

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: Mscorlib (in Mscorlib.dll)

See Also

System.Reflection Namespace

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.