src/AsyncDbg.Core/Core/TypeNameHelper.cs
/// <summary>
/// Makes a name of an async state machine type more readable.
/// </summary>
Makes a name of an async state machine type more readable.
Makes a name of an async state machine type more readable .
public static string GetAsyncMethodNameFromAsyncStateMachine(string originalTypeName)
        {
             2. Async state machine for an async lambda expression:
             like:
             ManualResetEventSlimOnTheStack.Program+<>c+<<RunAsync>b__1_0>d -> staticLambda1
             ManualResetEventSlimOnTheStack.Program+<>c+<<RunAsync>b__2_1>d -> staticLambda2
             Program+<>c__DisplayClass1_0.<<SetCompletion>b__0>d 
             WaitingOnTaskCompletionSource.Program+<>c__DisplayClass1_0.<SetCompletion>b__1 -> instanceLambda2
             3. Async state machine for a local async method:
             like: ManualResetEventSlimOnTheStack.Program+<<RunAsync>g__local|1_2>d
             4. Move next methods
             like: AsyncReaderWriterLockDeadlock.Program+<Main>d__1.MoveNext
            var regex = new Regex(@"(?<typeName>[^+]+)\+.*?<(?<asyncMethod>\w+)>(?<suffix>[^\.]+)(?<method>\.\w+)?");
            var match = regex.Match(originalTypeName);
            if (match.Success)
            {
                var typeName = match.Groups["typeName"].Value;
                var asyncMethod = match.Groups["asyncMethod"].Value;
                var method = match.Groups["method"].Value;
                var suffix = match.Groups["suffix"].Value;
                var lambdaKind = originalTypeName.Contains("+<>c+") ? "static" : originalTypeName.Contains("c__DisplayClass") ? "instance" : null;
                string? localName = suffix switch
                {
                     simple async method: d__1
                    _ when suffix.Contains("d__") => null,
                     lambda: b__1_0>d
                     Need to add 1 to get a meaningful number of a lambda expression within the method.
                    _ when suffix.Contains("b__") => $"{lambdaKind}Lambda" + (ExtractSuffixAsNumber(suffix, @"b__(?<result>\d).*")),
                     g__local|1_2>d
                    _ when suffix.Contains("g__") => Extract(suffix, @"g__(?<result>\w+)|.+?"),
                    _ => suffix,
                };
                var result = $"{typeName}.{asyncMethod}{method}";
                if (localName != null)
                {
                    result += "." + localName;
                }
                return result;
            }
            System.Console.WriteLine($"Can't match the state machine's name '{originalTypeName}'.");
            return originalTypeName;
        }
public static string GetAsyncMethodNameFromAsyncStateMachine ( string originalTypeName ) { var regex = new Regex ( @"(?<typeName>[^+]+)\+.*?<(?<asyncMethod>\w+)>(?<suffix>[^\.]+)(?<method>\.\w+)?" ) ; var match = regex . Match ( originalTypeName ) ; if ( match . Success ) { var typeName = match . Groups [ " " ] . Value ; var asyncMethod = match . Groups [ " " ] . Value ; var method = match . Groups [ " " ] . Value ; var suffix = match . Groups [ " " ] . Value ; var lambdaKind = originalTypeName . Contains ( " " ) ? " " : originalTypeName . Contains ( " " ) ? " " : null ; string ? localName = suffix switch { _ when suffix . Contains ( " " ) => null , _ when suffix . Contains ( " " ) => $" { lambdaKind } Lambda " + ( ExtractSuffixAsNumber ( suffix , @"b__(?<result>\d).*" ) ) , _ when suffix . Contains ( " " ) => Extract ( suffix , @"g__(?<result>\w+)|.+?" ) , _ => suffix , } ; var result = $" { typeName } . { asyncMethod } { method } " ; if ( localName != null ) { result += " " + localName ; } return result ; } System . Console . WriteLine ( $" Can't match the state machine's name ' { originalTypeName } '. " ) ; return originalTypeName ; }
Makes a name of an async state machine type more readable.
Makes a name of an async state machine type more readable .
// Possible cases: // 1. Async state machine for an instance/static method: // like: ManualResetEventSlimOnTheStack.Program+<RunAsync>d__1 // // 2. Async state machine for an async lambda expression: // like: // ManualResetEventSlimOnTheStack.Program+<>c+<<RunAsync>b__1_0>d -> staticLambda1 // ManualResetEventSlimOnTheStack.Program+<>c+<<RunAsync>b__2_1>d -> staticLambda2 // Program+<>c__DisplayClass1_0.<<SetCompletion>b__0>d  // WaitingOnTaskCompletionSource.Program+<>c__DisplayClass1_0.<SetCompletion>b__1 -> instanceLambda2 //  // 3. Async state machine for a local async method: // like: ManualResetEventSlimOnTheStack.Program+<<RunAsync>g__local|1_2>d // // 4. Move next methods // like: AsyncReaderWriterLockDeadlock.Program+<Main>d__1.MoveNext // simple async method: d__1 // lambda: b__1_0>d // Need to add 1 to get a meaningful number of a lambda expression within the method. // g__local|1_2>d
originalTypeName
string