sco*_*obi 3 powershell powershell-2.0
我一直在Powershell中使用add-type在我想使用的某些C#类中动态编译。效果很好,只限于2.0。
我刚刚发现了该-language csharpversion3选项,但不适用于-path。我该如何解决?
[编辑:删除了有关ReadAllText的内容-我弄错了。]
解决方法如下:以文本形式读取文件。
$script = [io.file]::readalltext($scriptpath)
add-type $script -lang csharpversion3
Run Code Online (Sandbox Code Playgroud)
我不妨粘贴其余内容,并以某种方式使此答案有用。.我有一个调试标志,可让我生成DLL,以便可以更轻松地插入调试器,并使用Reflector检查它,等等。
$cp = new-object codedom.compiler.compilerparameters
$cp.ReferencedAssemblies.Add('system.dll') > $null
$cp.ReferencedAssemblies.Add('system.core.dll') > $null
# optionally turn on debugging support
if ($debugscript)
{
# delete old unused crap while we're at it
dir "$($env:temp)\-*.dll" |%{
del $_ -ea silentlycontinue
if ($?) { del $_.fullname.replace('.dll', '.pdb') -ea silentlycontinue }
}
$cp.TreatWarningsAsErrors = $true
$cp.IncludeDebugInformation = $true
$cp.OutputAssembly = $env:temp + '\-' + [diagnostics.process]::getcurrentprocess().id + '.dll'
}
$script = [io.file]::readalltext($scriptpath)
add-type $script -lang csharpversion3 -compilerparam $cp
Run Code Online (Sandbox Code Playgroud)
如果$ debugscript设置为true,这将添加一些其他功能:
| 归档时间: |
|
| 查看次数: |
5705 次 |
| 最近记录: |