C# 加载程序集,带有通用引用 [英] C# Load Assembly w/ Common References

查看:20
本文介绍了C# 加载程序集,带有通用引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个小问题 - 我正在编写一个加载 DLL 的程序,每个 DLL 都包含一个类,该类继承自已加载的 DLL 和主机"程序引用的库中存在的类正在加载 DLL.这里的问题是,当我尝试加载并转换到超类时:

var assembly = Assembly.LoadFrom(dllPath);var type = assembly.GetTypes().FirstOrDefault(x => x.IsSubclassOf(typeof (MySuperclass)));...

虽然两者都引用包含 MySuperclass 的类,但由于 dll 引用了构建的类库(与加载程序引用的类库文件分开的文件)IsSubclassOf 永远不会返回 true,因为它认为两个类是不同的因为它们来自两个不同的程序集.

此外,如果您尝试将已加载类的创建实例转换为超类类型,则无法实现,因为它们不相同(不同的程序集).

所以,我的问题是:你如何处理引用通用代码的程序集,以便 c# 识别你正在加载一个继承自通用超类的类?

解决方案

如果您希望程序使用公共代码协同工作,您只需使用相同的程序集文件(即使它们相同).我是这样解决这个问题的:

程序正在从它自己的子目录加载一个 DLL.

文件夹结构:

MyApp 文件夹 -->我的程序通用依赖文件子模块 ->我的子模块.dll

要让 MySubmodule 使用下一个文件夹中的 CommonDependency.dll,这很简单.将 Visual Studio 配置为不将这些 dll 依赖项复制到构建文件夹.接下来,在 Visual Studio 中创建一个 App.config 并添加以下内容:

<预><代码><配置><运行时><程序集绑定><probing privatePath="../"/></assemblyBinding></运行时></配置>

这将告诉系统在父文件夹 ../ 中搜索程序集 - 如果您想要多个文件夹,可能是一个单独的依赖文件夹(相对于 .dll 的位置)) 您可以使用 ; 作为分隔符 - ../;../bin/; - 程序将在这些位置搜索依赖项.

I've run into a slight issue - I'm writing a program that loads DLLs, each of which contain a class which inherits from a class existing in a library referenced by both the loaded DLL and the "host" program that is loading the DLL. The issue here is that, when I try to load and cast to the superclass:

var assembly = Assembly.LoadFrom(dllPath);
var type = assembly.GetTypes().FirstOrDefault(x => x.IsSubclassOf(typeof (MySuperclass)));
...

Although both are referencing the class containing MySuperclass, since the dll is referencing the built class library (a separate file from the class library file the loading program is referencing) IsSubclassOf never returns true, because it considers the two classes to be different as they come from two different assemblies.

Furthermore, if you try casting a created instance of the loaded class to the superclass type, it can't do it since they're not the same (different assemblies).

So, my question is: how do you handle loading assemblies which reference common code, so that c# recognizes that you're loading a class which inherits from a common superclass?

解决方案

You simply must use the same assembly files (even if they are identical) if you want programs to work together using common code. Here's how I solved the issue:

The program is loading a DLL from a subdirectory of its own.

Folder structure:

MyApp Folder -->
     MyProgram.exe
     CommonDependency.dll
     Submodules ->
         MySubmodule.dll

To get MySubmodule to use CommonDependency.dll within the next folder up, it's quite simple. Configure Visual Studio to not copy these dll dependencies to the build folder. Next, create an App.config in Visual Studio and add the following:

<configuration>
  <runtime>
    <assemblyBinding>
      <probing privatePath="../"/>
    </assemblyBinding>
  </runtime>
</configuration>

This will tell the system to search for the assemblies in the parent folder ../ - if you want to have multiple folders, perhaps a separate dependency folder (relative to the location of the .dll) you can use ; as the delimiter - ../;../bin/; - the program will search these locations for the dependencies.

这篇关于C# 加载程序集,带有通用引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆