Powershell 如何将add type与-path和-language csharpversion3一起使用?
我一直在Powershell中使用add-type动态编译一些我想要使用的C#类。除了只有2.0版本外,其他都很好用 我刚刚发现了Powershell 如何将add type与-path和-language csharpversion3一起使用?,powershell,powershell-2.0,Powershell,Powershell 2.0,我一直在Powershell中使用add-type动态编译一些我想要使用的C#类。除了只有2.0版本外,其他都很好用 我刚刚发现了-language csharpversion3选项,但它不适用于-path。我怎样才能解决这个问题 [编辑:删除了关于ReadAllText的部分-我错了。]我不知道它是否正是您所需要的,但您可以在powershell中启用.NET 4支持。请记住,默认情况下它使用.NET2。为此,您需要在$PSHome中添加一个名为powershell.exe.config的XM
-language csharpversion3
选项,但它不适用于-path
。我怎样才能解决这个问题
[编辑:删除了关于ReadAllText的部分-我错了。]我不知道它是否正是您所需要的,但您可以在powershell中启用.NET 4支持。请记住,默认情况下它使用.NET2。为此,您需要在$PSHome中添加一个名为powershell.exe.config的XML,内容如下:
<?xml version="1.0"?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0.30319"/>
<supportedRuntime version="v2.0.50727"/>
</startup>
</configuration>
该方法使用Path.Combine和.NET4中定义的N个参数。在.NET2 Combine中,最多只能处理两个参数。我测试了另一个使用匿名委托的示例,这是C#3的特征之一:
祝你好运,祝你编码愉快。以下是解决方法:将文件作为文本读取
$script = [io.file]::readalltext($scriptpath)
add-type $script -lang csharpversion3
我不妨把剩下的粘贴进去,让这个答案在某种程度上有用。。我有一个调试标志,可以让我生成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
如果$debugscript设置为true,则会添加一些附加功能:
- 以警告作为错误进行编译
- 生成PDB
- 使用绑定到进程ID的特定DLL/PDB名称(在临时文件夹中),以便每个会话都有自己的名称。这有助于迭代对.cs的更改
- 删除旧的dll和pdb,在迭代时也很好
Slytherin>> $sharp = [IO.file]::readalltext((resolve-path new.cs))
Slytherin>> $sharp
using System.IO;
using System;
using System.Linq;
namespace grantest
{
public static class testeo4
{
public static string joinP(string[] arr)
{
arr.ToList<string>().ForEach(x=>Console.WriteLine(x));
return Path.Combine(arr);
}
}
}
Slytherin>> Add-Type -TypeDefinition $sharp
Add-Type : c:\Users\voodoomsr\AppData\Local\Temp\5ikkmpfn.0.cs(3) : The type or namesp
ace name 'Linq' does not exist in the namespace 'System' (are you missing an assembly
reference?)
c:\Users\voodoomsr\AppData\Local\Temp\5ikkmpfn.0.cs(2) : using System;
c:\Users\voodoomsr\AppData\Local\Temp\5ikkmpfn.0.cs(3) : >>> using System.Linq;
c:\Users\voodoomsr\AppData\Local\Temp\5ikkmpfn.0.cs(4) :
At line:1 char:9
+ Add-Type <<<< -TypeDefinition $sharp
+ CategoryInfo : InvalidData: (c:\Users\voodoo...bly reference?):Compile
rError) [Add-Type], Exception
+ FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddType
Command
Slytherin>> add-type -TypeDefinition $sharp -Language CSharpVersion3
Add-Type : c:\Users\voodoomsr\AppData\Local\Temp\v3r5whoc.0.cs(13) : No overload for m
ethod 'Combine' takes '1' arguments
c:\Users\voodoomsr\AppData\Local\Temp\v3r5whoc.0.cs(12) : arr.ToList<string>()
.ForEach(x=>Console.WriteLine(x));
c:\Users\voodoomsr\AppData\Local\Temp\v3r5whoc.0.cs(13) : >>> return Path.Comb
ine(arr);
c:\Users\voodoomsr\AppData\Local\Temp\v3r5whoc.0.cs(14) : }
At line:1 char:9
+ add-type <<<< -TypeDefinition $sharp -Language CSharpVersion3
+ CategoryInfo : InvalidData: (c:\Users\voodoo...s '1' arguments:Compile
rError) [Add-Type], Exception
+ FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddType
Command
Slytherin>> Add-Type -TypeDefinition $sharp -ReferencedAssemblies System.core
Slytherin>> [grantest.testeo4]::joinP($arr)
uno
dos
tres
cuatro
cinco
uno\dos\tres\cuatro\cinco
$script = [io.file]::readalltext($scriptpath)
add-type $script -lang csharpversion3
$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