@@ -2736,20 +2736,30 @@ public override InterfaceMapping GetInterfaceMap([DynamicallyAccessedMembers(Dyn
2736
2736
im . InterfaceMethods = new MethodInfo [ ifaceVirtualMethodCount ] ;
2737
2737
im . TargetMethods = new MethodInfo [ ifaceVirtualMethodCount ] ;
2738
2738
2739
+ int actualCount = 0 ;
2739
2740
for ( int i = 0 ; i < ifaceVirtualMethodCount ; i ++ )
2740
2741
{
2741
2742
RuntimeMethodHandleInternal ifaceRtMethodHandle = RuntimeTypeHandle . GetMethodAt ( ifaceRtType , i ) ;
2742
2743
2744
+ // GetMethodAt may return null handle for methods that do not exist or are not supposed
2745
+ // to be seen in reflection. One example is async variant methods.
2746
+ // We do not record mapping for interface methods that do not exist.
2747
+ if ( ifaceRtMethodHandle . IsNullHandle ( ) )
2748
+ continue ;
2749
+
2743
2750
// GetMethodBase will convert this to the instantiating/unboxing stub if necessary
2744
2751
MethodBase ifaceMethodBase = GetMethodBase ( ifaceRtType , ifaceRtMethodHandle ) ! ;
2745
2752
Debug . Assert ( ifaceMethodBase is RuntimeMethodInfo ) ;
2746
- im . InterfaceMethods [ i ] = ( MethodInfo ) ifaceMethodBase ;
2753
+ im . InterfaceMethods [ actualCount ] = ( MethodInfo ) ifaceMethodBase ;
2747
2754
2748
2755
// If the impl is null, then virtual stub dispatch is active.
2749
2756
RuntimeMethodHandleInternal classRtMethodHandle = TypeHandle . GetInterfaceMethodImplementation ( ifaceRtTypeHandle , ifaceRtMethodHandle ) ;
2750
2757
2751
2758
if ( classRtMethodHandle . IsNullHandle ( ) )
2759
+ {
2760
+ actualCount ++ ;
2752
2761
continue ;
2762
+ }
2753
2763
2754
2764
// If we resolved to an interface method, use the interface type as reflected type. Otherwise use `this`.
2755
2765
RuntimeType reflectedType = RuntimeMethodHandle . GetDeclaringType ( classRtMethodHandle ) ;
@@ -2764,10 +2774,16 @@ public override InterfaceMapping GetInterfaceMap([DynamicallyAccessedMembers(Dyn
2764
2774
// the TargetMethod provided to us by runtime internals may be a generic method instance,
2765
2775
// potentially with invalid arguments. TargetMethods in the InterfaceMap should never be
2766
2776
// instances, only definitions.
2767
- im . TargetMethods [ i ] = ( targetMethod is { IsGenericMethod : true , IsGenericMethodDefinition : false } )
2777
+ im . TargetMethods [ actualCount ++ ] = ( targetMethod is { IsGenericMethod : true , IsGenericMethodDefinition : false } )
2768
2778
? targetMethod . GetGenericMethodDefinition ( ) : targetMethod ! ;
2769
2779
}
2770
2780
2781
+ if ( actualCount != ifaceVirtualMethodCount )
2782
+ {
2783
+ Array . Resize ( ref im . InterfaceMethods , actualCount ) ;
2784
+ Array . Resize ( ref im . TargetMethods , actualCount ) ;
2785
+ }
2786
+
2771
2787
return im ;
2772
2788
}
2773
2789
#endregion
0 commit comments