我正在尝试在 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) ...
我在这里想念什么?
编辑1:
问题似乎出在 AppDomainSetup 中的委托上,Powershell 从中创建了一个动态方法。有没有办法改变这种行为?