【问题标题】:PowerShell: Checking for Loaded Assembly before trying to Load it again ('Assembly with same name is already loaded')PowerShell:在尝试再次加载之前检查加载的程序集(\'已加载同名的程序集\')
【发布时间】:2022-10-22 01:19:15
【问题描述】:

我在 PowerShell 7 SDK 环境(由使用它的 .NET 应用程序提供的运行空间)中运行,我正在手动加载 .NET DLL 以使用以下代码使用它们的功能:

if(([System.AppDomain]::CurrentDomain.GetAssemblies() | Where-Object { $_.ManifestModule.Name -eq 'HtmlAgilityPack.dll' }).Count -eq 0) {
    [Reflection.Assembly]::LoadFrom("C:\ProgramData\ExampleApp\HtmlAgilityPack.dll") | Out-Null
}

此代码在同一个 .NET 实例的多个运行空间中执行,但是,我最终得到错误:

Assembly with same name is already loaded

很明显,我检查它是否已经加载不正常,正确的检查是什么?

【问题讨论】:

    标签: powershell .net-6.0


    【解决方案1】:

    您可以检查 Assembly 中的某个类是否已加载 GetAssembly,并且仅在失败时才加载程序集:

    try
    {
        $null=[System.Reflection.Assembly]::GetAssembly([Kusto.Data.KustoConnectionStringBuilder])
    }
    catch
    {
        [System.Reflection.Assembly]::LoadFrom("$(Get-Location)inKusto.Data.dll") | Out-Null
    }
    

    【讨论】:

      猜你喜欢
      • 2021-05-31
      • 2021-03-02
      • 2018-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多