我试图在Powershell脚本中创建一个AppDomain . 我在C#中实现了完全相同,它似乎工作正常 . 但是,Powershell版本总是失败,但有例外 . 脚本代码是:
function Execute($arguments)
{
Write-Host "Arguments: " $arguments[0] $arguments[1] $arguments[2]
}
function Main
{
$appDomain = $null
Try
{
$appDomainSetup = New-Object -TypeName System.AppDomainSetup
$appDomainSetup.AppDomainInitializer = ${function:Execute}
$appDomainSetup.AppDomainInitializerArguments = @("Test1", "Test2", "Test3")
$appDomain = [AppDomain]::CreateDomain("TestDomain", $null, $appDomainSetup)
}
Finally
{
If ($appDomain -ne $null)
{
[AppDomain]::Unload($appDomain)
}
}
}
Main
例外情况是:未处理的异常:System.AccessViolationException:尝试读取或写入受保护的内存 . 这通常表明其他内存已损坏 . 在System.AppDomain.nCreateDomain(String friendlyName,AppDomainSetup setup,Evidence providedSecurityInfo,Evidence creatorsSecurityInfo,IntPtr parentSecurityDescriptor)...
我在这里想念的是什么?
Edit 1:
问题似乎与AppDomainSetup中的委托有关,Powershell从中创建了一个动态方法 . 有没有办法改变这种行为?