【问题标题】:How to load an assembly as reflection-only in a new AppDomain?如何在新的 AppDomain 中将程序集加载为仅反射?
【发布时间】:2016-05-16 22:00:52
【问题描述】:

我在 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 之类的程序集上运行该函数时,出现以下错误:

使用“1”参数调用“ReflectionOnlyLoadFrom”的异常:“API 限制:
程序集 'file:///C:\Program Files (x86)\Reference Assemblies\Microsoft\
Framework\.NETFramework\v4.6.1\System.Xaml.dll' 已经从
不同的位置。它不能从同一个新位置加载
应用程序域。”
在 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 要点,因此其他人可以重现错误/测试更改。

【问题讨论】:

  • 错误说明一切,程序集已加载,跳过这些并从 System.Reflection.Assembly.GetExecutingAssembly().GetReferencedAssemblies() 中获取它们
  • 如果您的目标只是检查元数据,那么有很多替代方案,例如 Mono.Cecil。涉及新的 AppDomain 和反射太复杂了。

标签: .net powershell .net-assembly appdomain


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我想我明白了:

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

    【讨论】:

    • 这似乎不起作用 - 它只会在控制台上回显 DoCallback 的文字内容
    猜你喜欢
    • 2010-09-27
    • 1970-01-01
    • 2011-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多