@@ -45,6 +45,7 @@ internal class Instrumenter
4545 private List < string > _excludedSourceFiles ;
4646 private List < string > _branchesInCompiledGeneratedClass ;
4747 private List < ( MethodDefinition , int ) > _excludedMethods ;
48+ private List < string > _excludedLambdaMethods ;
4849 private List < string > _excludedCompilerGeneratedTypes ;
4950 private readonly string [ ] _doesNotReturnAttributes ;
5051 private ReachabilityHelper _reachabilityHelper ;
@@ -508,12 +509,18 @@ private void InstrumentType(TypeDefinition type)
508509 continue ;
509510 }
510511
512+ if ( _excludedLambdaMethods != null && _excludedLambdaMethods . Contains ( method . FullName ) )
513+ {
514+ continue ;
515+ }
516+
511517 if ( ! customAttributes . Any ( IsExcludeAttribute ) )
512518 {
513519 InstrumentMethod ( method ) ;
514520 }
515521 else
516522 {
523+ ( _excludedLambdaMethods ??= new List < string > ( ) ) . AddRange ( CollectLambdaMethodsInsideLocalFunction ( method ) ) ;
517524 ( _excludedMethods ??= new List < ( MethodDefinition , int ) > ( ) ) . Add ( ( method , ordinal ) ) ;
518525 }
519526 }
@@ -850,6 +857,19 @@ internal bool IsSynthesizedNameOf(string name, string methodName, int methodOrdi
850857 ( name . IndexOf ( $ "<{ methodName } >g__") != - 1 && name . IndexOf ( $ "|{ methodOrdinal } _") != - 1 ) ;
851858 }
852859
860+ private static IEnumerable < string > CollectLambdaMethodsInsideLocalFunction ( MethodDefinition methodDefinition )
861+ {
862+ if ( ! methodDefinition . Name . Contains ( ">g__" ) ) yield break ;
863+
864+ foreach ( Instruction instruction in methodDefinition . Body . Instructions . ToList ( ) )
865+ {
866+ if ( instruction . OpCode == OpCodes . Ldftn && instruction . Operand is MethodReference mr && mr . Name . Contains ( ">b__" ) )
867+ {
868+ yield return mr . FullName ;
869+ }
870+ }
871+ }
872+
853873 /// <summary>
854874 /// A custom importer created specifically to allow the instrumentation of System.Private.CoreLib by
855875 /// removing the external references to netstandard that are generated when instrumenting a typical
0 commit comments