如何在Powershell中从.net framework调用重载静态方法?

如何在Powershell中从.net framework调用重载静态方法?,powershell,overloading,Powershell,Overloading,下面是我试过的和发生的事情的记录 我正在寻找如何调用一个特定的重载,并解释为什么下面的方法不起作用。如果你的答案是,你应该使用这个命令,或者叫它两次,请理解我不接受你的答案 PS C:\> [System.IO.Path]::Combine("C:\", "foo") C:\foo PS C:\> [System.IO.Path]::Combine("C:\", "foo", "bar") Cannot find an overload for "Combine" and the a

下面是我试过的和发生的事情的记录

我正在寻找如何调用一个特定的重载,并解释为什么下面的方法不起作用。如果你的答案是,你应该使用这个命令,或者叫它两次,请理解我不接受你的答案

PS C:\> [System.IO.Path]::Combine("C:\", "foo")
C:\foo
PS C:\> [System.IO.Path]::Combine("C:\", "foo", "bar")
Cannot find an overload for "Combine" and the argument count: "3".
At line:1 char:26
+ [System.IO.Path]::Combine <<<< ("C:\", "foo", "bar")
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest

PS C:\> [System.IO.Path]::Combine(, "C:\", "foo", "bar")
Missing ')' in method call.
At line:1 char:27
+ [System.IO.Path]::Combine( <<<< , "C:\", "foo", "bar")
    + CategoryInfo          : ParserError: (CloseParenToken:TokenId) [], Paren
   tContainsErrorRecordException
    + FullyQualifiedErrorId : MissingEndParenthesisInMethodCall

PS C:\> [System.IO.Path]::Combine($("C:\", "foo", "bar"))
Cannot find an overload for "Combine" and the argument count: "1".
At line:1 char:26
+ [System.IO.Path]::Combine <<<< ($("C:\", "foo", "bar"))
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest
哪个Powershell将调用该特定重载?Path.Combine具有以下两种功能:

public static string Combine (string path1, string path2, string path3);
public static string Combine (params string[] paths);

这两个都可以,还是只有一个?显然,在这种特定情况下,很难区分两者之间的区别。

在这种情况下,您需要创建一个params数组,并对其调用Combine方法。。参数数组可以按如下方式创建:@C:\,foo,bar

我认为,以下事件作为参数C:\、foo、bar应该调用第二个方法

我不知道您对Path有什么困惑。Combine有两个重载方法,一个是combinign两个字符串,另一个是接受一组参数的参数数组,powershell中的第二种情况处理方式与c不同

希望这能回答你的问题


仅供参考,我是powershell noob..

接受多个参数的路径重载仅在.NET 4及更高版本中可用。您需要创建一个配置文件,告诉Powershell使用.NET 4启动,这将允许您访问这些方法

在$pshome中创建名为powershell.exe.config的文件,该文件包含以下内容:

<?xml version="1.0"?> 
<configuration> 
    <startup useLegacyV2RuntimeActivationPolicy="true"> 
        <supportedRuntime version="v4.0.30319"/> 
        <supportedRuntime version="v2.0.50727"/> 
    </startup> 
</configuration>

要添加,请发出以下命令:

[System.IO.Path]::Combine.OverloadDefinitions
$PSVersionTable
从shell中,您应该获得以下输出:

static string Combine(string path1, string path2)
如您所见,没有可用的重载

发出命令:

[System.IO.Path]::Combine.OverloadDefinitions
$PSVersionTable

看看CLRVersion,你会发现你使用的是4.0之前的.net版本,所以Path.Combine没有重载

这些重载是.NET4.0中新增的。您确定Powershell正在.Net 4.0下运行吗?您可以使用[environment]::version获取CLR版本。也可以安装并使用已在.NET 4.0上运行的powershell 3.0。