@@ -45,6 +45,7 @@ internal class Instrumenter
45
45
private List < string > _excludedSourceFiles ;
46
46
private List < string > _branchesInCompiledGeneratedClass ;
47
47
private List < ( MethodDefinition , int ) > _excludedMethods ;
48
+ private List < string > _excludedLambdaMethods ;
48
49
private List < string > _excludedCompilerGeneratedTypes ;
49
50
private readonly string [ ] _doesNotReturnAttributes ;
50
51
private ReachabilityHelper _reachabilityHelper ;
@@ -508,12 +509,18 @@ private void InstrumentType(TypeDefinition type)
508
509
continue ;
509
510
}
510
511
512
+ if ( _excludedLambdaMethods != null && _excludedLambdaMethods . Contains ( method . FullName ) )
513
+ {
514
+ continue ;
515
+ }
516
+
511
517
if ( ! customAttributes . Any ( IsExcludeAttribute ) )
512
518
{
513
519
InstrumentMethod ( method ) ;
514
520
}
515
521
else
516
522
{
523
+ ( _excludedLambdaMethods ??= new List < string > ( ) ) . AddRange ( CollectLambdaMethodsInsideLocalFunction ( method ) ) ;
517
524
( _excludedMethods ??= new List < ( MethodDefinition , int ) > ( ) ) . Add ( ( method , ordinal ) ) ;
518
525
}
519
526
}
@@ -850,6 +857,19 @@ internal bool IsSynthesizedNameOf(string name, string methodName, int methodOrdi
850
857
( name . IndexOf ( $ "<{ methodName } >g__") != - 1 && name . IndexOf ( $ "|{ methodOrdinal } _") != - 1 ) ;
851
858
}
852
859
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
+
853
873
/// <summary>
854
874
/// A custom importer created specifically to allow the instrumentation of System.Private.CoreLib by
855
875
/// removing the external references to netstandard that are generated when instrumenting a typical
0 commit comments