您好我的应用程序由一些“内核”和模块组成,这些模块动态加载到自己的 AppDomain 并根据需要卸载。但我不明白一件事。我将发布一个与我所拥有的类似的简单代码。
有一个“接口”类,其中包含许多虚拟钩子和属性以及一些自己的功能:
[Serializable()]
public abstract class Module : MarshalByRefObject
{
public static List<Module> module = new List<Module>();
public string Name = "";
public string Version = "unknown";
public DateTime Date = DateTime.Now;
Module()
{
lock(module)
{
module.Add(this);
}
}
~Module()
{
Exit();
lock (module)
{
if (module.Contains(this))
{
module.Remove(this);
}
}
core.Log("Module was unloaded: " + this.Name);
}
public virtual void Hook1()
{
// by default ignore
}
}
each module is a new project that is referenced to core and creates a new class that is inherited from Module and extends the "kernel". Some events are hooked in a way that I go in a loop through Module.module List that is supposed to contain all existing loaded instances and call the respective hook in that one. This works fine if I am in one AppDomain. But if I use a separate AppDomain for module it seems that the memory of original Domain get copied to new domain and when I call the constructor of new instance, it doesn't insert itself to static array in Module.module of original domain, but it insert itself to Module.module in a new domain. That means, in core I still have 0 modules in Module.module. I managed to fix this by creating another function that register module instead of what I had in constructor. But still it happens that sometimes I access the memory in original domain and sometime the memory in new domain of module. This causes troubles. How can I make it sure that I always access the same memory from kernel and from module as well?
Example of what I need to do is:
- Pass pointer to instance of a class which is in memory of AppDomain A to a hook of module living in AppDomain B
- let the module change something in that class (in memory of domain A, not in B)