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

时间:2016-02-07 03:24:40

标签: .net powershell .net-assembly 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这样的方法,我不能/不想做,因为这只是反射。

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

编辑: Here是我所制作的脚本的GitHub要点,因此其他人可以重复错误/测试更改。

2 个答案:

答案 0 :(得分:0)

我想我已经明白了:

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

答案 1 :(得分:0)

为不同的目标框架加载多个程序集时,我遇到了相同的异常,但是具有相同的标识。我的解决方法是在单独的.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...

check-assembly.ps1:

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

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