加载程序集没有锁定文件,并保持正确的绑定上下文 [英] Load an assembly without locking file AND keep the right Binding Context

查看:260
本文介绍了加载程序集没有锁定文件,并保持正确的绑定上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我COMP对一个强大的难题。这里是我的情况:

I've comp up against a formidable conundrum. Here's my situation:

我要建一个插件框架的应用程序。有一个基础的插件类,所有的插件必须扩展。在同一个相同的组件,我将序列化和反序列化类的一个辅助类。这是一个通用类,并使用它所有的地方。其结构是这样的:

I'm building an application with a plugin framework. There's a base plugin class that all plugins must extend. Within the same same assembly I have a helper class that will serialize and deserialize classes. It's a generic class and it's used all over the place. The structure is like this:

MyApp.dll
|_ App.cs
|_ HelperCollection.cs
|_ PluginBase.cs

MyPlugin.dll
|_MyPlugin.cs (this extends PluginBase)
|_Foo.cs

问题

我的问题是程序集加载和锁定的文件。该应用程序的一个要求是,插件的,可以在任何时候覆盖。如果是这样,他们需要被重新加载。它似乎加载程序集的最好方式,以便它没有被锁定(即我可以将其覆盖或吹走,而应用程序仍在运行)是这样的:

The Problem

My problem is the assembly loading and locking files. A requirement of the app is that Plugins can be overwritten at any time. If so, they need to be reloaded. It seems the best way to load an assembly so that it's not locked (that is, I can overwrite it or blow away while the app is still running) is this:

byte[] readAllBytes = File.ReadAllBytes("MyPlugin.dll");
Assembly assembly = Assembly.Load(readAllBytes);

加载插件组件工作得很好,有没有问题。我得到一个异常时,从内部的 MyPlugin.cs 的,这是在插件组装,我试图用 HelperCollection 做反序列化。一个例子可能是这样的:

Loading the plugin assembly works just fine, no problems there. I get an exception when, from within MyPlugin.cs, which is in the plugin assembly, I tried to use the HelperCollection to do deserialization. An example could be something like this:

// HelperCollection uses XmlSerializer under the covers
List<Foo> settingCollection = HelperCollection<Foo>.Deserialize("mysettings.xml");

它吹起来,并抛出一个 InvalidCastException的说这是无法投类型的对象列表[美孚]'到'名单[富]。经过大量的研究我终于找到了为什么。它得到装入 LoadNeither绑定上下文

It's blowing up and throwing an InvalidCastException saying that it's "Unable to cast object of type 'List[Foo]' to 'List[Foo]'". After much research I finally found why. It's getting loaded in the LoadNeither binding context.

终止(从 MyPlugin.dll 的),它在 LoadNeither 加载结合上下文的组件包含了类型转换的类型,而(在我的情况下,的 MyApp.dll 的),在默认情况下被加载。因此即使它们具有相同的名称,他们不考虑相同的类型。这是因为我使用的是这样做的Assembly.Load(byte []的)

When Foo is loaded (from MyPlugin.dll) it's in the LoadNeither binding context while the assembly containing the type for the type conversion (in my case, MyApp.dll) is loaded in Default context. So even though they have the same name they are not considered the same type. It's doing this because I'm using Assembly.Load(byte[]).

我怎样才能解决这个?哪有我,

How can I get around this? How can I,

  1. 在加载程序集,而不是锁定文件和
  2. 提供正确的结合上下文,这样我可以施放位于加载的程序集的对象。

抱歉文本的墙上,只是想获得所有相关的信息在那里。

Sorry for the wall of text, just wanted to get all the relevant info out there.

推荐答案

您是否尝试过阴影复制

当你使用影子拷贝,复制。NET装配到一个临时目录,并从那里加载(所以它是一个的锁定的临时文件,而不是原来的组件) - 但是所有的约束力的规则是基于原始文件的位置。

When you're using shadow copying, .NET copies the assembly to a temp directory and loads it from there (so it's the temp file that's locked, not the original assembly) -- but all the binding rules are based on the original file location.

这篇关于加载程序集没有锁定文件,并保持正确的绑定上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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