如何在新的 AppDomain 中将程序集加载为仅反射?

How to load an assembly as reflection-only in a new AppDomain?

我有C#经验,但对AppDomain等概念比较陌生。无论如何,我试图让一个程序集加载到仅反射上下文中,这样我就可以获取它的所有名称空间。这是我现在的代码(警告:PowerShell):

function Get-Namespaces($assembly)
{
    $assemblyClass = [Reflection.Assembly]
    $winmdClass = [Runtime.InteropServices.WindowsRuntime.WindowsRuntimeMetadata]
    $domain = [AppDomain]::CurrentDomain

    # Since desktop .NET can't work with winmd files,
    # we have to use the reflection-only APIs and preload
    # all the dependencies manually.
    $appDomainHandler =
    {
        Param($sender, $e);
        $assemblyClass::ReflectionOnlyLoad($e.Name)
    }

    $winmdHandler =
    {
        Param($sender, $e)
        [string[]] $empty = @()
        $path = $winmdClass::ResolveNamespace($e.NamespaceName, $empty) | select -Index 0
        $e.ResolvedAssemblies.Add($assemblyClass::ReflectionOnlyLoadFrom($path))
    }

    # Hook up the handlers
    $domain.add_ReflectionOnlyAssemblyResolve($appDomainHandler)
    $winmdClass::add_ReflectionOnlyNamespaceResolve($winmdHandler)

    # Do the actual work
    $assemblyObject = $assemblyClass::ReflectionOnlyLoadFrom($assembly)
    $types = $assemblyObject.GetTypes()
    $namespaces = $types | ? IsPublic | select Namespace -Unique

    # Deregister the handlers
    $domain.remove_ReflectionOnlyAssemblyResolve($appDomainHandler)
    $winmdClass::remove_ReflectionOnlyNamespaceResolve($winmdHandler)

    return $namespaces
}

出于某种原因,当我在 运行 像 System.XamlWindowsBase 这样的程序集上运行函数时,我遇到了这些错误:

Exception calling "ReflectionOnlyLoadFrom" with "1" argument(s): "API restriction:
The assembly 'file:///C:\Program Files (x86)\Reference Assemblies\Microsoft\
Framework\.NETFramework\v4.6.1\System.Xaml.dll' has already loaded from a
different location. It cannot be loaded from a new location within the same
appdomain."
At C:\Users\James\Code\csharp\Shims.Xaml\generate.ps1:50 char:5
+     $assemblyObject = $assemblyClass::ReflectionOnlyLoadFrom($assembl ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : FileLoadException

所以我想知道的是,如何在新的 AppDomain 中加载程序集?我检查了 MSDN,但我能找到的只有像 CreateInstanceAndUnwrap 这样的方法,我不能t/don 不想这样做,因为这是仅反射。

TL;DR: 如何在新 AppDomain 的仅反射上下文中加载程序集?欢迎使用 C# 和 PowerShell 代码示例。


编辑: Here 是我编写的脚本的 GitHub 要点,因此其他人可以复制 error/test 更改。

我想我明白了:

function Load-AssemblyInNewAppDomain($assembly)
{
    $domain = [AppDomain]::CreateDomain("foo")
    $domain.DoCallback
    ({
        $loaded = [Reflection.Assembly]::Load($assembly)
    })
}

我在为不同的目标框架加载多个程序集时遇到相同的异常,但具有相同的标识。我的解决方法是在单独的 .ps1 中执行程序集加载,并通过 CMD /C 调用它,这会为每个脚本调用创建一个新的 AppDomain,从而避免异常。

父级 *.ps1:

& CMD /C powershell.exe -NoProfile -ExecutionPolicy Bypass -File "$PsScriptRoot\check-assembly.ps1" "net461\MyAssembly.dll"
# Check exit code...
& CMD /C powershell.exe -NoProfile -ExecutionPolicy Bypass -File "$PsScriptRoot\check-assembly.ps1" "netcoreapp3.1\MyAssembly.dll"
# Check exit code...

检查程序集。ps1:

Param(
    [Parameter(Mandatory, HelpMessage="DLL file to check: ")] 
    [string]$dllFile
)

$ass = [System.Reflection.Assembly]::LoadFrom("$dllFile")
# Check $ass