使用AppDomain加载程序集

时间:2015-03-26 10:07:01

标签: c# .net-assembly appdomain

回答我的问题,我已经在stackoverflow,但是,我找不到合适的/不能让它工作。

我尝试做的是将.dll加载到我的Windows窗体,使用反射来检索方法/类名。我使用Assembly.LoadFrom让它工作但是它正在完成我想要的操作,因为这会锁定.dll文件。

我已经阅读并发现AppDomain可以做到这一点,但是我无法让它去做。

以下是我尝试过的代码,但在CreateInstanceFromAndUnwrap中,我不知道它正在寻找哪个typeName

var executableFilePath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);

AppDomain domain = AppDomain.CreateDomain(
               DllPath,
                null,
                new AppDomainSetup
                {
                    ApplicationBase = Path.GetPathRoot(executableFilePath),
                    PrivateBinPathProbe = string.Empty,

                    PrivateBinPath = string.Join(";", DllPath, executableFilePath),
                    ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile,
                });



      var temp = domain.CreateInstanceFromAndUnwrap(DllPath, typeof(????).FullName, false,
            BindingFlags.Default, null, null, null, null);

我将在运行时加载的.dll将不会被引用到我的Windows窗体应用程序中。

1 个答案:

答案 0 :(得分:0)

尝试查看此主题:How to Load an Assembly to AppDomain with all references recursively?

方法CreateInstanceFromAndUnwrap从您的typeof(????)返回透明代理.FullName。所以你应该:

  1. 创建一些继承自导入dll中的MarshalByRefObject的类
  2. 使用typeof(%yourNewMarshalByRefClassNameFrom1%)。FullName