dotnet / dotnet/runtime

[API Proposal]: DiagnosticMethodInfo should return full method signature

Open
#123,308 5 comments 0 reactions 0 assignees View on GitHub
api-suggestion area-System.Diagnostics needs-further-triage
Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Description

### Background and motivation

With .NET 10, `DiagnosticMethodInfo` does not allow you to obtain method parameters, which can be problematic when identifying the actual method called in the stack when its parameters are overloaded.

The parameter types would be sufficient if the parameter names cannot be reliably retrieved in an AOT context.

```csharp
try
{
throw new InvalidOperationException("oops");
}
catch (Exception ex)
{
StackFrame[] frames = new StackTrace(ex, true).GetFrames();
for (int i = 0; i < frames.Length; i++)
{
StackFrame frame = frames[i];
DiagnosticMethodInfo? methodInfo = DiagnosticMethodInfo.Create(frame);
string methodName = $"{methodInfo?.DeclaringTypeName}.{methodInfo?.Name}";
// there is no way to get full method signaure with parameters from DiagnosticMethodInfo

Console.WriteLine($"Method: {methodName}");
}
}
```

Returns `AssamblyName.ClassName.MethodName`

### API Proposal

```csharp
namespace System.Diagnostics
{
public sealed class DiagnosticMethodInfo
{
public string Name { get; }
public string NameWithParams { get; } // new API
public string DeclaringTypeName { get; }
public string DeclaringAssemblyName { get; }
public static DiagnosticMethodInfo? Create(Delegate @delegate);
public static DiagnosticMethodInfo? Create(StackFrame frame);
}
}
```

### API Usage

```csharp
try
{
throw new InvalidOperationException("oops");
}
catch (Exception ex)
{
StackFrame[] frames = new StackTrace(ex, true).GetFrames();
for (int i = 0; i < frames.Length; i++)
{
StackFrame frame = frames[i];
DiagnosticMethodInfo? methodInfo = DiagnosticMethodInfo.Create(frame);
string methodName = $"{methodInfo?.DeclaringTypeName}.{methodInfo?.NameWithParams}";

Console.WriteLine($"Method: {methodName}");
}
}
```

Should return `AssamblyName.ClassName.MethodName(String)`

### Alternative Designs

_No response_

### Risks

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.