您需要在另一个 appdomain 中加载该类型。通常这是通过将派生自 MarshalByRefObject 的类型加载到另一个域中,将实例编组到原始域并通过代理执行方法来完成的。这听起来更难,所以这里是例子:
public class Helper : MarshalByRefObject // must inherit MBRO, so it can be "remoted"
{
public void RegisterAssembly()
{
// load your assembly here and do what you need to do
var asm = Assembly.LoadFrom("c:\\test.dll", null);
// do whatever...
}
}
static class Program
{
static void Main()
{
// setup and create a new appdomain with shadowcopying
AppDomainSetup setup = new AppDomainSetup();
setup.ShadowCopyFiles = "true";
var domain = AppDomain.CreateDomain("loader", null, setup);
// instantiate a helper object derived from MarshalByRefObject in other domain
var handle = domain.CreateInstanceFrom(Assembly.GetExecutingAssembly().Location, typeof (Helper).FullName);
// unwrap it - this creates a proxy to Helper instance in another domain
var h = (Helper)handle.Unwrap();
// and run your method
h.RegisterAssembly();
AppDomain.Unload(domain); // strictly speaking, this is not required, but...
...
}
}