Assembly.GetTypes() - ReflectionTypeLoadException
我们为我们的应用程序实现一个插件框架,并使用 Assembly.Loadfrom 加载插件程序集。然后,我们使用 GetTypes() 并进一步检查每个插件文件的类型以获取支持的接口。
插件的路径由用户提供,我们循环浏览文件夹中的每个文件以查看它(插件)是否支持我们的插件接口。如果是,我们创建一个实例,如果不是,我们将转到下一个文件。
我们从一个代码库构建两个版本的软件(appA_1 和 appA_2)。
当插件由与插件文件同时构建的应用程序加载时,加载插件效果很好。但是,如果我们构建 appA_2 并指向 appA_1 的插件文件夹,则在调用 GetTypes() 时会出现异常。
我们代码的基本版本是:
var pluginAssembly = Assembly.LoadFrom(FileName);
foreach (var pluginType in pluginAssembly.GetTypes())
{
我们收到“ReflectionTypeLoadException”异常。
这是令人担忧的,因为我们希望我们的应用程序能够加载由任何人构建的任何插件的类型。我们还缺少什么吗?
编辑: 遍历 LoaderExceptions 后,我们发现有一个文件 libPublic.dll 生成了 System.IO.FileNotFoundException 异常。奇怪的是,这个文件驻留在应用程序目录中,而插件引用了项目文件。
编辑2: 在异常日志中我们发现以下内容 “比较程序集名称导致不匹配:修订号”
We implement a plugin framework for our application and load plugin assemblies using Assembly.Loadfrom. We then use GetTypes() and further examine the types with each plugin file for supported Interfaces.
A path for the plugins is provided by the user and we cycle through each of the files in the folder to see if it (the plugin) supports our plugin interface. If it does, we create an instance, if not we move onto the next file.
We build two versions of software from the one code base (appA_1 and appA_2).
Loading the plugins works well when the plugins are loaded by the application that was built at the same time as the plugin file. However if we build appA_2 and point to the plugin folder of appA_1, we get an exception when GetTypes() is called.
A basic version of our code is;
var pluginAssembly = Assembly.LoadFrom(FileName);
foreach (var pluginType in pluginAssembly.GetTypes())
{
We get a "ReflectionTypeLoadException" exception.
This is concerning because we want our application to be able to load the types of any plugin, built by anyone. Is there something we are missing?
EDIT:
After iterating through the LoaderExceptions we have discovered that there is a single file libPublic.dll that generates a System.IO.FileNotFoundException exception. The strange thing is that this file resides in the application directory and the plugin is referenced to the project file.
EDIT 2:
In the exception log we find the following
"Comparing the assembly name resulted in the mismatch: Revision Number"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一些事情:
确保插件目录中没有重复的程序集(即您已经从应用程序目录加载到主应用程序中的程序集。)否则,当您加载插件时,它可能会加载同一程序集的附加副本。这可能会导致有趣的异常,例如:
<块引用>
对象(类型为“MyObject”)不是“MyObject”类型。
如果您在实例化类型时遇到异常,您可能需要处理
AppDomain.AssemblyResolve
:我意识到必须告诉 CLR 有点奇怪,为了解析程序集,请找到具有我们用来解析的名称的程序集,但我发现如果没有它,就会发生奇怪的事情。例如,我可以从插件程序集中实例化类型,但如果我尝试使用
TypeDescriptor.GetConverter
,它将找不到该类的TypeConverter
,即使它可以看到类上的Converter
属性。查看您的编辑,这可能不是导致当前异常的原因,尽管您稍后在使用插件时可能会遇到这些问题。
A few things:
Make sure you don't have duplicate assemblies in the plugin directory (i.e. assemblies that you're already loading in your main app from your app directory.) Otherwise, when you load your plugin, it may load an additional copy of the same assembly. This can lead to fun exceptions like:
If you're getting the exception when instantiating a type, you may need to handle
AppDomain.AssemblyResolve
:I realize it's a bit strange to have to tell the CLR that, in order to resolve an assembly, find the assembly with the name we're using to resolve, but I've seen odd things happen without it. For example, I could instantiate types from a plugin assembly, but if I tried to use
TypeDescriptor.GetConverter
, it wouldn't find theTypeConverter
for the class, even though it could see theConverter
attribute on the class.Looking at your edits, this is probably not what's causing your current exception, though you may run into these issues later as you work with your plugins.
感谢这篇文章,我可以解决在
UITypeEditor
中遇到的ReflectionTypeLoadException
问题。它是自定义类库的设计器程序集(设计时使用的 winforms 智能标记),可扫描某些类型。Thanks to this post I could solve the
ReflectionTypeLoadException
that I was getting in aUITypeEditor
. It's a designer assembly (a winforms smart-tag used at design-time) of a custom class library, that scan for some types.您遇到程序集版本不匹配的情况。由于您的插件引用了此 libPublic.dll,因此您必须仔细对其进行版本控制,特别是不要更改其修订版/构建/等。每次编译时的数字。
You are getting an assembly version mismatch. Since your plugins refer to this
libPublic.dll
, you must version it carefully and in particular not bump its revision/build/etc. numbers at every compile.