How to call overloaded static methods from .NET Framework in Powershell?

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

I am looking for how to cause a specific overload, along with an explanation of why the following does not work. If your answer is “you should use this command character instead” or “call it twice”, please understand when I do not 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 

this is what i do in c # that works:

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

What will Powershell cause this particular overload? Path.Combine has both of these options:

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

Is it possible to call both of them or only one? Obviously, in this particular case it is difficult to tell the difference.

+4
source share
3 answers

Path overloads that take multiple arguments that are only available in .NET 4 and later. You need to create a configuration file to tell Powershell to start using .NET 4, which will give you access to these methods.

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> 
+7
source

To add, it issues the command:

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

from your shell, you should get the following result:

 static string Combine(string path1, string path2) 

As you can see, there are no overloads.

Run the command:

 $PSVersionTable 

and look at CLRVersion and you’ll see that you are using .net version up to 4.0, so there are no overloads available for Path.Combine.

+4
source

In this case, you need to create an array of params and call the Combine method on it. An array of params can be created as follows: @("C:\", "foo", "bar")

I believe that the following event as the parameter "C:\,foo,bar" should call the second method.

I'm not sure that you are confused in Path.Combine has two overloaded methods: one is a combination of two lines, and the other is an array of parameters that takes a bunch of arguments, the second case in powershell is handled differently than C #.

Hope this answers your question.

FYI, I am powershell noob ..

+1
source

Source: https://habr.com/ru/post/1435588/


All Articles