powershell 2 new-object“找不到类型...”从外部库实例化 .net 类实现的接口时出现异常

发布于 2024-10-03 02:34:46 字数 750 浏览 2 评论 0原文

我发现了一个很容易重现的问题。请告知是否有任何解决办法?

有两个.Net 库libraryA.dll 和libraryB.dll。每个库都有一个接口InterfaceA和InterfaceB。 ClassAA 实现 InterfaceA 并位于libraryA 中。 ClassAB 实现 InterfaceB 并位于同一库 A 中。同理ClassBB - LibraryB - InterfaceB; ClassBA - LibraryB - InterfaceA

当实例化 ClassAA 和 ClassBB 但实例化 ClassAB 或 ClassBA 时, New-Object 可以正常工作。他们总是无法实例化。

这是 powershell 代码

[System.Reflection.Assembly]::LoadFile(‘c:\LibraryA.dll’)
[System.Reflection.Assembly]::LoadFile(‘c:\LibraryB.dll’)

$obj1 = new-object -typeName ClassAA   (IT WORKS)
$obj2 = new-object -typeName ClassBB   (IT WORKS)
$obj3 = new-object -typeName ClassAB   (EXCEPTION THROWN)
$obj4 = new-object -typeName ClassBA   (EXCEPTION THROWN)

非常感谢,

Andrey

I have found an issue that is quite easy to reproduce. Please advise if there is any work around for this?

There are two .Net libraries libraryA.dll and libraryB.dll. And each library has one interface InterfaceA and InterfaceB. ClassAA implements InterfaceA and lives in libraryA. ClassAB implements InterfaceB and lives in the same libraryA. The same way ClassBB - LibraryB - InterfaceB; ClassBA - LibraryB - InterfaceA

New-Object works correctly when ClassAA and ClassBB are instantiated but not ClassAB or ClassBA. They constantly fail to instantiate.

Here you are powershell code

[System.Reflection.Assembly]::LoadFile(‘c:\LibraryA.dll’)
[System.Reflection.Assembly]::LoadFile(‘c:\LibraryB.dll’)

$obj1 = new-object -typeName ClassAA   (IT WORKS)
$obj2 = new-object -typeName ClassBB   (IT WORKS)
$obj3 = new-object -typeName ClassAB   (EXCEPTION THROWN)
$obj4 = new-object -typeName ClassBA   (EXCEPTION THROWN)

Many Thanks,

Andrey

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

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

发布评论

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

评论(2

无悔心 2024-10-10 02:34:46

使用 ::LoadFile 而不是 ::LoadFile

[System.Reflection.Assembly]::LoadFrom(‘c:\LibraryA.dll’)
[System.Reflection.Assembly]::LoadFrom(‘c:\LibraryB.dll’)

当您使用 时: :LoadFrom 程序集将被加载到其加载目录的上下文中,同一目录中引用的程序集将自动被解析。 ::LoadFile 用于加载共享标识但位于不同的目录,并且不保留任何加载上下文,因此不会解析引用的程序集。

Instead of ::LoadFile, use:

[System.Reflection.Assembly]::LoadFrom(‘c:\LibraryA.dll’)
[System.Reflection.Assembly]::LoadFrom(‘c:\LibraryB.dll’)

When you use ::LoadFrom the assembly will be loaded into a context with the directory it was loaded from, referenced assemblies in that same directory will automatically be resolved. ::LoadFile is meant for loading assemblies that share an identity but are in different directories and does not keep any load context, so referenced assemblies are not resolved.

故事与诗 2024-10-10 02:34:46

这个问题的答案解决了你的问题:
如何获取要使用的 PowerShell 添加类型添加类型

关键是使用AppDomain.CurrentDomain.AssemblyResolve事件。

例如,您可以将 AssemblyResolver 类(来自上面的帖子)添加到 LibraryA,然后在需要时使用 [Utils.AssemblyResolver]::AddAssemblyLocation("LibraryB.dll") 拉入 LibraryB 引用。

或者,只是为了证明这一点:

[System.AppDomain]::CurrentDomain.add_assemblyResolve({
    If ($args[1].Name.StartsWith("LibraryB"))
    {
        Return [System.Reflection.Assembly]::LoadFile("C:\LibraryB.dll")
    }
    Else
    {
        Return $Null
    }
})

请注意,上面的示例中存在循环依赖关系:LibraryA 引用 LibraryB,而 LibraryB 引用 LibraryA。您可能想首先解决这个问题 - 假设您在实际项目中也有相同的问题......

The answer to this question solves your issue:
How can I get PowerShell Added-Types to use Added Types

The key is to use the AppDomain.CurrentDomain.AssemblyResolve event.

You can, for example, add the AssemblyResolver class (from the post above) to your LibraryA and then use [Utils.AssemblyResolver]::AddAssemblyLocation("LibraryB.dll") to pull in the LibraryB reference when needed.

Or, just to prove the point:

[System.AppDomain]::CurrentDomain.add_assemblyResolve({
    If ($args[1].Name.StartsWith("LibraryB"))
    {
        Return [System.Reflection.Assembly]::LoadFile("C:\LibraryB.dll")
    }
    Else
    {
        Return $Null
    }
})

Note that you have a circular dependency in your example above: LibraryA is referencing LibraryB and LibraryB references LibraryA. You will probably want to fix that first - assuming you have the same in your real project...

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