@@ -191,14 +191,20 @@ private void ValidateAllAbstractMethodsAreImplemented()
191191 }
192192
193193 [ UnconditionalSuppressMessage ( "ReflectionAnalysis" , "IL2065:DynamicallyAccessedMembers" , Justification = "Methods are loaded from this TypeBuilder. The interface methods should be available at this point" ) ]
194+ [ UnconditionalSuppressMessage ( "ReflectionAnalysis" , "IL2075:DynamicallyAccessedMembers" , Justification =
195+ "Somehow #pragma warning disable IL2075 doesn't suppress anymore, related to https://github.com/dotnet/runtime/issues/96646" ) ]
194196 [ UnconditionalSuppressMessage ( "ReflectionAnalysis" , "IL2085:DynamicallyAccessedMembers" , Justification = "Methods are loaded from this TypeBuilder" ) ]
195197 private void CheckInterfaces ( Type [ ] _interfaces )
196198 {
197199 foreach ( Type interfaceType in _interfaces )
198200 {
199- #pragma warning disable IL2075 // Analyzer produces a different warning code than illink. The IL2065 suppression takes care of illink: https://github.com/dotnet/runtime/issues/96646
200- MethodInfo [ ] interfaceMethods = interfaceType . GetMethods ( BindingFlags . Public | BindingFlags . NonPublic | BindingFlags . Instance | BindingFlags . Static ) ;
201- #pragma warning restore IL2075
201+ Type ifaceType = interfaceType ;
202+ if ( interfaceType . IsConstructedGenericType &&
203+ IsConstructedFromTypeBuilder ( interfaceType . GetGenericTypeDefinition ( ) , interfaceType . GetGenericArguments ( ) ) )
204+ {
205+ ifaceType = interfaceType . GetGenericTypeDefinition ( ) ;
206+ }
207+ MethodInfo [ ] interfaceMethods = ifaceType . GetMethods ( BindingFlags . Public | BindingFlags . NonPublic | BindingFlags . Instance | BindingFlags . Static ) ;
202208 for ( int i = 0 ; i < interfaceMethods . Length ; i ++ )
203209 {
204210 MethodInfo interfaceMethod = interfaceMethods [ i ] ;
@@ -207,7 +213,9 @@ private void CheckInterfaces(Type[] _interfaces)
207213 continue ;
208214 }
209215
210- MethodInfo ? implementedMethod = GetMethodImpl ( interfaceMethod . Name , GetBindingFlags ( interfaceMethod ) , null , interfaceMethod . CallingConvention , GetParameterTypes ( interfaceMethod . GetParameters ( ) ) , null ) ;
216+ Type [ ] parameterTypes = interfaceMethod . ContainsGenericParameters ?
217+ GetParameterTypesMatchGeneric ( interfaceMethod . GetParameters ( ) , interfaceType . GetGenericArguments ( ) ) : GetParameterTypes ( interfaceMethod . GetParameters ( ) ) ;
218+ MethodInfo ? implementedMethod = GetMethodImpl ( interfaceMethod . Name , GetBindingFlags ( interfaceMethod ) , null , interfaceMethod . CallingConvention , parameterTypes , null ) ;
211219
212220 if ( ( implementedMethod == null || implementedMethod . IsAbstract ) && ! FoundInInterfaceMapping ( interfaceMethod ) )
213221 {
@@ -216,12 +224,28 @@ private void CheckInterfaces(Type[] _interfaces)
216224 }
217225
218226 // Check parent interfaces too
219- #pragma warning disable IL2075 // Analyzer produces a different warning code than illink. The IL2065 suppression takes care of illink: https://github.com/dotnet/runtime/issues/96646
220- CheckInterfaces ( interfaceType . GetInterfaces ( ) ) ;
221- #pragma warning restore IL2075
227+ CheckInterfaces ( ifaceType . GetInterfaces ( ) ) ;
222228 }
223229 }
224230
231+ private static bool IsConstructedFromTypeBuilder ( Type constructedType , Type [ ] genericArgs )
232+ {
233+ if ( constructedType is TypeBuilderImpl )
234+ {
235+ return true ;
236+ }
237+
238+ foreach ( Type arg in genericArgs )
239+ {
240+ if ( arg is TypeBuilderImpl or GenericTypeParameterBuilderImpl )
241+ {
242+ return true ;
243+ }
244+ }
245+
246+ return false ;
247+ }
248+
225249 private bool FoundInInterfaceMapping ( MethodInfo abstractMethod )
226250 {
227251 if ( _methodOverrides == null )
@@ -836,8 +860,6 @@ internal static BindingFlags GetBindingFlags(MethodInfo method)
836860
837861 private static bool MatchesTheFilter ( MethodBuilderImpl method , BindingFlags methodFlags , BindingFlags bindingFlags , CallingConventions callConv , Type [ ] ? argumentTypes )
838862 {
839- bindingFlags ^= BindingFlags . DeclaredOnly ;
840-
841863 if ( ( bindingFlags & methodFlags ) != methodFlags )
842864 {
843865 return false ;
@@ -977,19 +999,17 @@ public override MethodInfo[] GetMethods(BindingFlags bindingAttr)
977999 }
9781000
9791001 [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . PublicFields | DynamicallyAccessedMemberTypes . NonPublicFields ) ]
980- public override FieldInfo ? GetField ( string name , BindingFlags bindingAttr )
1002+ public override FieldInfo ? GetField ( string name , BindingFlags bindingFlags )
9811003 {
9821004 ArgumentNullException . ThrowIfNull ( name ) ;
1005+ ThrowIfNotCreated ( ) ;
9831006
9841007 FieldInfo ? match = null ;
985-
986- BindingFlags fieldFlags = bindingAttr ^ BindingFlags . DeclaredOnly ;
987- fieldFlags ^= BindingFlags . IgnoreCase ;
988- StringComparison compare = ( bindingAttr & BindingFlags . IgnoreCase ) != 0 ? StringComparison . OrdinalIgnoreCase : StringComparison . Ordinal ;
1008+ StringComparison compare = ( bindingFlags & BindingFlags . IgnoreCase ) != 0 ? StringComparison . OrdinalIgnoreCase : StringComparison . Ordinal ;
9891009 foreach ( FieldBuilderImpl fieldInfo in _fieldDefinitions )
9901010 {
991- BindingFlags currentFieldFlags = GetBindingFlags ( fieldInfo ) ;
992- if ( name . Equals ( fieldInfo . Name , compare ) && ( fieldFlags & currentFieldFlags ) == currentFieldFlags )
1011+ BindingFlags fieldFlags = GetBindingFlags ( fieldInfo ) ;
1012+ if ( name . Equals ( fieldInfo . Name , compare ) && ( bindingFlags & fieldFlags ) == fieldFlags )
9931013 {
9941014 if ( match != null )
9951015 {
@@ -1003,9 +1023,9 @@ public override MethodInfo[] GetMethods(BindingFlags bindingAttr)
10031023 }
10041024 }
10051025
1006- if ( match == null && ! bindingAttr . HasFlag ( BindingFlags . DeclaredOnly ) && _typeParent != null )
1026+ if ( match == null && ! bindingFlags . HasFlag ( BindingFlags . DeclaredOnly ) && _typeParent != null )
10071027 {
1008- match = _typeParent . GetField ( name , bindingAttr ) ;
1028+ match = _typeParent . GetField ( name , bindingFlags ) ;
10091029 }
10101030
10111031 return match ;
@@ -1022,7 +1042,23 @@ private static BindingFlags GetBindingFlags(FieldBuilderImpl field)
10221042 }
10231043
10241044 [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . PublicFields | DynamicallyAccessedMemberTypes . NonPublicFields ) ]
1025- public override FieldInfo [ ] GetFields ( BindingFlags bindingAttr ) => throw new NotSupportedException ( ) ;
1045+ public override FieldInfo [ ] GetFields ( BindingFlags bindingAttr )
1046+ {
1047+ ThrowIfNotCreated ( ) ;
1048+
1049+ List < FieldBuilderImpl > candidates = new List < FieldBuilderImpl > ( _fieldDefinitions . Count ) ;
1050+ for ( int i = 0 ; i < _fieldDefinitions . Count ; i ++ )
1051+ {
1052+ FieldBuilderImpl fieldInfo = _fieldDefinitions [ i ] ;
1053+ BindingFlags fieldFlags = GetBindingFlags ( fieldInfo ) ;
1054+ if ( ( bindingAttr & fieldFlags ) == fieldFlags )
1055+ {
1056+ candidates . Add ( fieldInfo ) ;
1057+ }
1058+ }
1059+
1060+ return candidates . ToArray ( ) ;
1061+ }
10261062
10271063 [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . Interfaces ) ]
10281064 [ return : DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . Interfaces ) ]
@@ -1105,6 +1141,21 @@ private static Type[] GetParameterTypes(ParameterInfo[] parameterInfos)
11051141 return parameterTypes ;
11061142 }
11071143
1144+ private static Type [ ] GetParameterTypesMatchGeneric ( ParameterInfo [ ] parameters , Type [ ] genericArguments )
1145+ {
1146+ Type [ ] parameterTypes = new Type [ parameters . Length ] ;
1147+ for ( int i = 0 ; i < parameters . Length ; i ++ )
1148+ {
1149+ parameterTypes [ i ] = parameters [ i ] . ParameterType ;
1150+ if ( parameterTypes [ i ] . IsGenericParameter )
1151+ {
1152+ parameterTypes [ i ] = genericArguments [ parameterTypes [ i ] . GenericParameterPosition ] ;
1153+ }
1154+ }
1155+
1156+ return parameterTypes ;
1157+ }
1158+
11081159 private void ValidateInterfaceType ( Type interfaceType )
11091160 {
11101161 if ( ! interfaceType . IsInterface )
0 commit comments