mardi 8 août 2017

Calling AppDomain.DoCallback from Powershell

This is based on the Stack Overflow question: How to load an assembly as reflection-only in a new AppDomain?

I am attempting to determine the runtime version of an assembly, but that assembly could be loaded multiple times as I traverse through nested folders. Loading the assembly directly using

[Reflection.Assembly]::ReflectionOnlyLoadFrom($assembly)

will therefore not work, as the assembly can only be loaded once in the app-domain.

Given the following function to load an assembly in a separate AppDomain:

function Load-AssemblyInNewAppDomain($assembly)
{
    Write-Host $assembly.FullName
    $domain = [AppDomain]::CreateDomain([Guid]::NewGuid())
    $domain.DoCallback
    ({
        $loaded = [Reflection.Assembly]::Load($assembly)
        $runtime = $loaded.ImageRuntimeVersion
        Write-Host $runtime
    })
}

This outputs the contents of the delegate to the console, rather than executing it:

OverloadDefinitions
-------------------
void DoCallBack(System.CrossAppDomainDelegate callBackDelegate)
void _AppDomain.DoCallBack(System.CrossAppDomainDelegate theDelegate)


        $loaded = [Reflection.Assembly]::Load($assembly)

        $runtime = $loaded.ImageRuntimeVersion
        Write-Host $runtime

Note that the results are the same, whether I use PowerShell 4 or 5

Any help/guidance appreciated





Aucun commentaire:

Enregistrer un commentaire