Several people suggested using the call stack to get information about the module. If you want to get the type of object that made the call, this is not so bad. Unfortunately, it is not possible to implement (i.e., a Simple, portable, and functional) method for retrieving instance data from data in the call stack. There are several reasons, including optimization problems, which use methods that then do not appear on the stack ... that may also interfere with the recognition of the type of call.
Given this fact, the short answer is that you should provide a Module object as a parameter for the methods that you call in the DLL. The method suggested by @pswg is one way to achieve this, but it has the disadvantage of potentially polluting the Symbol class symbol space. This can be resolved if the Module class implements a protected or public API member that provides the functions you want to provide:
public abstract class Module { internal int ID; public class APIWrapper { Module module; public APIWrapper(Module module) { this.module = module; } public void method1() { apiimpl.method1(this.module); } public int method2() { return apiimpl.method2(this.module); } } public readonly APIWrapper API; public Module() { ID = generate_module_identity(); API = new APIWrapper(this); } public abstract void ModuleStart(); } internal static class apiimpl { public static void method1(Module module) { ... } public static int method2(Module module) { ... } }
Other developers may use it as follows:
class MyModule : Module { public override void ModuleStart() { API.method1(); } }
This encapsulates the methods that your DLL provides without introducing too much pollution into the symbol space of the Module class hierarchy.
[Type of opinion = "mine" value = "YMMV"]
However, I suggest that you seriously review the use of this type of abstraction. If the methods you call require some information about the specific instance of the Module that calls them, it should be clear in the method parameters. Encouraging your team to follow recommendations that lead to clarity may be more important than finding ways to abstract out small details.
[/opinion]
source share