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-onl
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)
})
}