您需要在另一個應用程序域中加載該類型。通常這是通過將從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...
...
}
}
什麼是出現的錯誤。 – gideon
您無法卸載程序集,但可以卸載應用程序域。 –