【问题标题】:Using AppDomain to load assembly使用 AppDomain 加载程序集
【发布时间】:2015-03-26 10:07:00
【问题描述】:

我已经在 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 窗体应用程序中。

【问题讨论】:

  • 您的 Forms-App 是否在同一个 App-Domain 中?新创建的和加载的内容将不会自动用于初始应用程序域
  • 如何检查?

标签: c# .net-assembly appdomain


【解决方案1】:

试试看这个帖子:How to Load an Assembly to AppDomain with all references recursively?

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

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

【讨论】:

  • 我尝试了该问题的最佳答案,并在我做value.GetAssembly(ReferenceDll)...时不断给我和错误。无法加载文件或程序集Gamers_V1_0_0, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified
  • 尝试检查您的本地目录路径,它可能会有所不同。 var currentDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); var assemblyFullPath = Path.Combine(currentDir, "assembly name");
  • 我已经尝试过if(File.Exists 但是它通过了并且给了我同样的错误
  • 也许程序集有一些依赖项,他们没有复制到构建文件夹?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多