Skip to content

Add docs for new .NET MethodInvocation tracing #12226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions reference/7.6/Microsoft.PowerShell.Core/About/about_Methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,40 @@ specific overload of the **Bar** method.
int: 1
```

## Finding what overload was used

Since PowerShell 7.6, it is now possible to see what method overload was chosen
by PowerShell. This is achieved through the new `MethodInvocation` tracing
command.

This example shows how to use `Trace-Command` to display the overload chosen
when calling the [String.Split method](/dotnet/api/system.string.split).

```powershell
Trace-Command -PSHost -Name MethodInvocation -Expression {
"a 1 b 1 c 1 d".Split(1)
}

Trace-Command -PSHost -Name MethodInvocation -Expression {
"a 1 b 1 c 1 d".Split("1")
}
```

```Output
DEBUG: ... MethodInvocation Information: 0 : Invoking method: string[] Split(char separator, System.StringSplitOptions options = System.StringSplitOptions.None)

a 1 b 1 c 1 d

DEBUG: ... MethodInvocation Information: 0 : Invoking method: string[] Split(string separator, System.StringSplitOptions options = System.StringSplitOptions.None)
a
b
c
d
```

In the first example the `1` was casted as a `[char]` and not a string causing
the split output to split by `[char]1` and not the character `"1"`.

## Using .NET methods that take filesystem paths

PowerShell supports multiple runspaces per process. Each runspace has its own
Expand All @@ -275,3 +309,4 @@ method.
- [about_Member-Access_Enumeration](about_Member-Access_Enumeration.md)
- [about_Properties](about_Properties.md)
- [Get-Member](xref:Microsoft.PowerShell.Utility.Get-Member)
- [Trace-Command](xref:Microsoft.PowerShell.Utility.Trace-Command)