@@ -191,14 +191,20 @@ private void ValidateAllAbstractMethodsAreImplemented()
191
191
}
192
192
193
193
[ 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" ) ]
194
196
[ UnconditionalSuppressMessage ( "ReflectionAnalysis" , "IL2085:DynamicallyAccessedMembers" , Justification = "Methods are loaded from this TypeBuilder" ) ]
195
197
private void CheckInterfaces ( Type [ ] _interfaces )
196
198
{
197
199
foreach ( Type interfaceType in _interfaces )
198
200
{
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 ) ;
202
208
for ( int i = 0 ; i < interfaceMethods . Length ; i ++ )
203
209
{
204
210
MethodInfo interfaceMethod = interfaceMethods [ i ] ;
@@ -207,7 +213,9 @@ private void CheckInterfaces(Type[] _interfaces)
207
213
continue ;
208
214
}
209
215
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 ) ;
211
219
212
220
if ( ( implementedMethod == null || implementedMethod . IsAbstract ) && ! FoundInInterfaceMapping ( interfaceMethod ) )
213
221
{
@@ -216,12 +224,28 @@ private void CheckInterfaces(Type[] _interfaces)
216
224
}
217
225
218
226
// 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 ( ) ) ;
222
228
}
223
229
}
224
230
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
+
225
249
private bool FoundInInterfaceMapping ( MethodInfo abstractMethod )
226
250
{
227
251
if ( _methodOverrides == null )
@@ -836,8 +860,6 @@ internal static BindingFlags GetBindingFlags(MethodInfo method)
836
860
837
861
private static bool MatchesTheFilter ( MethodBuilderImpl method , BindingFlags methodFlags , BindingFlags bindingFlags , CallingConventions callConv , Type [ ] ? argumentTypes )
838
862
{
839
- bindingFlags ^= BindingFlags . DeclaredOnly ;
840
-
841
863
if ( ( bindingFlags & methodFlags ) != methodFlags )
842
864
{
843
865
return false ;
@@ -977,19 +999,17 @@ public override MethodInfo[] GetMethods(BindingFlags bindingAttr)
977
999
}
978
1000
979
1001
[ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . PublicFields | DynamicallyAccessedMemberTypes . NonPublicFields ) ]
980
- public override FieldInfo ? GetField ( string name , BindingFlags bindingAttr )
1002
+ public override FieldInfo ? GetField ( string name , BindingFlags bindingFlags )
981
1003
{
982
1004
ArgumentNullException . ThrowIfNull ( name ) ;
1005
+ ThrowIfNotCreated ( ) ;
983
1006
984
1007
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 ;
989
1009
foreach ( FieldBuilderImpl fieldInfo in _fieldDefinitions )
990
1010
{
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 )
993
1013
{
994
1014
if ( match != null )
995
1015
{
@@ -1003,9 +1023,9 @@ public override MethodInfo[] GetMethods(BindingFlags bindingAttr)
1003
1023
}
1004
1024
}
1005
1025
1006
- if ( match == null && ! bindingAttr . HasFlag ( BindingFlags . DeclaredOnly ) && _typeParent != null )
1026
+ if ( match == null && ! bindingFlags . HasFlag ( BindingFlags . DeclaredOnly ) && _typeParent != null )
1007
1027
{
1008
- match = _typeParent . GetField ( name , bindingAttr ) ;
1028
+ match = _typeParent . GetField ( name , bindingFlags ) ;
1009
1029
}
1010
1030
1011
1031
return match ;
@@ -1022,7 +1042,23 @@ private static BindingFlags GetBindingFlags(FieldBuilderImpl field)
1022
1042
}
1023
1043
1024
1044
[ 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
+ }
1026
1062
1027
1063
[ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . Interfaces ) ]
1028
1064
[ return : DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . Interfaces ) ]
@@ -1105,6 +1141,21 @@ private static Type[] GetParameterTypes(ParameterInfo[] parameterInfos)
1105
1141
return parameterTypes ;
1106
1142
}
1107
1143
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
+
1108
1159
private void ValidateInterfaceType ( Type interfaceType )
1109
1160
{
1110
1161
if ( ! interfaceType . IsInterface )
0 commit comments