如何从 Powershell 中的 .net 框架调用重载的静态方法? [英] How do I call overloaded static methods from the .net framework in Powershell?

查看:49
本文介绍了如何从 Powershell 中的 .net 框架调用重载的静态方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我尝试过的和发生的事情的记录.

Below is a transcript of what I've tried and what happens.

我正在寻找如何调用特定的重载以及解释为什么以下内容不起作用.如果您的回答是您应该改用这个命令行开关"或调用两次",请理解我不接受您的回答.

I'm looking for how to call a specific overload along with an explanation of why the following does not work. If your answer is "you should use this commandlet instead" or "call it twice" please understand when I don't accept your answer.

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

这是我在 c# 中所做的,它有效:

this is what I do in c#, which works:

var foobar = Path.Combine(@"C:\", "foo", "bar");
Console.WriteLine(foobar);

什么 Powershell 会调用那个特定的重载?Path.Combine 具有以下两种功能:

What Powershell will invoke that specific overload? Path.Combine has both of these:

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

是否可以同时调用这两个或一个?显然,在这种特定情况下,很难区分.

Is it possible to call both of these, or just one? Obviously, in this specific case, it's difficult to tell the difference.

推荐答案

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

The Path overloads that accept multiple arguments like that are only available in .NET 4 and up. You need to create a config file to tell Powershell to launch using .NET 4, which will give you access to those methods.

在 $pshome 中创建一个名为powershell.exe.config"的文件,内容如下:

Create a file called "powershell.exe.config" in $pshome with the following contents:

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

这篇关于如何从 Powershell 中的 .net 框架调用重载的静态方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆