如果库的名称已更改,AppDomain.CreateInstanceAndUnwrap 将失败

发布于 2024-08-17 07:04:05 字数 748 浏览 2 评论 0原文

您好,

我有一个应用程序,允许用户导入他们创建的库(.NET DLL),只要该库符合我给他们的特定准则(使用我的命名空间、用我的属性装饰方法等) 。我将每个用户库复制到内部目录,然后将其加载到自己的应用程序域中(以便用户可以根据需要卸载它)。因此,我有一个限制,即不能加载两个同名的库。

我想消除此限制,而不将每个用户库放入内部目录的唯一子目录中。当我将用户库复制到内部目录时,我尝试重命名它。例如,如果用户说导入 c:\SomeLib.dll 并且我检测到已经加载了一个名为 SomeLib.dll 的库,我会将新文件复制到 ...\MyInternalDir\SomeLib2.dll。但是,当我这样做时,我的加载命令:

(ISomeInterface) iSomeLib = someAppDomain.CreateInstanceAndUnwrap(
                                    "SomeLib2", 
                                    "SomeNamespace.SomeClassInSomeLib");

抛出异常:

FileLoadException:无法加载文件 或程序集“MyLib2”或其之一 依赖关系。所在的程序集 清单定义不匹配 装配参考。 (例外情况来自 HRESULT:0x80131040)

是否有某种方法告诉.NET“忽略文件名自编译以来已更改的事实”?

Greetings,

I have an application that allows users to import libraries (.NET DLLs) they've created, as long as the library conforms to specific guidelines I've given them (use my namespace, decorate methods with my attribute, etc.). I copy each user lib to an internal directory and then load it into its own app domain (so user can unload it if desired). Therefore, I have the limitation that you can't load two libs with the same name.

I'd like to remove this limitation without placing each user lib into a unique subdirectory of my internal directory. I tried renaming the user lib when I copy it to my internal directory. For example, if the user says to import c:\SomeLib.dll and I detect I already have a lib named SomeLib.dll loaded, I copy the new file to ...\MyInternalDir\SomeLib2.dll. However, when I do this, my load command:

(ISomeInterface) iSomeLib = someAppDomain.CreateInstanceAndUnwrap(
                                    "SomeLib2", 
                                    "SomeNamespace.SomeClassInSomeLib");

throws an exception:

FileLoadException: Could not load file
or assembly 'MyLib2' or one of its
dependencies. The located assembly's
manifest definition does not match the
assembly reference. (Exception from
HRESULT: 0x80131040)

Is there some way to tell .NET "Ignore that fact that the file name has been changed since it was compiled"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花辞树 2024-08-24 07:04:05

您是否考虑过尝试重载:

someAppDomain.CreateInstanceFrom(string assemblyFile, string typeName)

鉴于您知道新的 dll 名称,并且进行了重命名,您可以要求应用程序域从指定的 dll 创建实例。这样,.Net 就不应该关心程序集名称和程序集文件名是否匹配。

我应该以“我还没有真正尝试过”作为开头,但这只是一个建议!

Have you considered trying the overload:

someAppDomain.CreateInstanceFrom(string assemblyFile, string typeName)

Given that you know the new dll name, since you did the rename, you can ask the App Domain to create the instance from the specified dll. That way it .Net shouldn't care whether the assembly name and the assembly filename match.

I should have prefaced this with "I haven't actually tried it", but it is just a suggestion!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文