简体   繁体   中英

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

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
}

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 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

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: How can I load assemblies in a reflection-only context in a new AppDomain? Both C# and PowerShell code samples welcome.


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

I got the same exception when loading multiple assemblies for different target frameworks, but with the same identity. My workaround was to do the assembly load in a separate .ps1, and call it via CMD /C , which creates a new AppDomain for each script call, thereby avoiding the exception.

Parent *.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

I think I've got it:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM