如何在新的 AppDomain 中将程序集加载为仅反射? [英] How to load an assembly as reflection-only in a new AppDomain?

查看:22
本文介绍了如何在新的 AppDomain 中将程序集加载为仅反射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 C# 方面有经验,但对 AppDomain 等概念相对不熟悉.无论如何,我试图让一个程序集在仅反射的上下文中加载,以便我可以获取其所有名称空间.这是我现在拥有的代码(警告:PowerShell):

I'm experienced in C# but relatively unfamiliar with the concepts of AppDomain and the like. Anyway, I'm trying to get an assembly to load in a reflection-only context so I can grab all of its namespaces. Here is the code I have right now (warning: 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 等程序集上运行该函数时,我收到以下错误:

For some reason, when I'm running the function on assemblies like System.Xaml or WindowsBase I'm getting these errors:

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

那么我想知道的是,如何将程序集加载到新的 AppDomain 中?我检查了 MSDN 但我只能找到是像 CreateInstanceAndUnwrap 这样的方法,我不能/不想这样做,因为这只是反射.

So what I'd like to know is, how can I load the assembly in a new AppDomain? I checked MSDN but all I can find are methods like CreateInstanceAndUnwrap, which I can't/don't want to do since this is reflection-only.

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

TL;DR: How can I load assemblies in a reflection-only context in a new AppDomain? Both C# and PowerShell code samples welcome.

这里是我的脚本的 GitHub 要点已经做了,所以其他人可以重现错误/测试更改.

Here is a GitHub gist of the script I've made, so others can repro the error/test changes.

推荐答案

我想我明白了:

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

这篇关于如何在新的 AppDomain 中将程序集加载为仅反射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆