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

编程入门 行业动态 更新时间:2024-10-18 22:29:04
本文介绍了如何在新的 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.Xaml 或 WindowsBase 等程序集上运行该函数时,我收到以下错误:

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 中将程序集加载为仅反射?

本文发布于:2023-11-06 13:56:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563885.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中将   反射   加载   程序   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!