1
0
Files
2024-03-25 17:39:44 +08:00

40 lines
1.4 KiB
C#

using CoreProfiler;
using System.Reflection;
using System.Runtime.CompilerServices;
namespace CoreProfilerExample.Common.Extensions
{
public static class MethodBaseExtension
{
public static IDisposable ProfilingStep(this MethodBase method, string? stepName = null)
{
if (stepName == null)
{
// 取得方法的類型物件
var methodType = method.ReflectedType;
// 取得類別的類型物件
var classType = methodType?.ReflectedType ?? methodType;
// 取出類別底下所有非同步類型的方法,比對出當前的方法類型
var classMethodType = classType?
.GetMethods()
.Select(x => new
{
Method = x,
Attribute = x.GetCustomAttribute<AsyncStateMachineAttribute>(),
})
.FirstOrDefault(x => x.Attribute?.StateMachineType == methodType);
var className = classType?.Name ?? "無法取得類別名稱";
var methodName = classMethodType?.Method.Name ?? method.Name ?? "無法取得方法名稱";
stepName = $"{classType?.Name}.{methodName}";
}
return ProfilingSession.Current.Step(stepName);
}
}
}