Powershell 如何将add type与-path和-language csharpversion3一起使用?

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

我一直在Powershell中使用add-type动态编译一些我想要使用的C#类。除了只有2.0版本外,其他都很好用

我刚刚发现了
-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,在迭代时也很好

是的,我有自己的Powershell,配置为支持4.0。但它不是很便携。我不能要求这里所有使用我脚本的人都对他们的本地Powershell进行黑客攻击。更不用说启用4.0支持会显著降低启动时间。谢谢你的回答,但我想我的公主在另一座城堡里。是的,它的速度慢得令人难以置信。因为这个原因,我有两个powershell可执行文件,一个配置了2runtime,另一个配置了4runtime。我在开发时使用4,需要测试和使用代码,在需要快速运行一些2runtime脚本时使用版本2。只需复制可执行文件并将其命名为例如powershell2.exe,为该可执行文件创建仅启用2运行时的powershell2.exe.config,:)
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