vendredi 1 décembre 2023

c# Enumerable Reflection GetMethod not found method [duplicate]

I have code for getting method Select for Enumerable.

var selectMethod = typeof(Enumerable).GetMethod("Select", BindingFlags.Public | BindingFlags.Static, [typeof(IEnumerable<>), typeof(Func<,>)]);

I getting null. But when i debbugging in source code was method as candidate with exact types as IEnumerable and Func but they were slightly different like they don't have AssemblyQualifiedName and some other fields, it was RuntimeType or something as i know, and as result type which getting in source code is not assignable from type what in method parameters.

I tried other types, but it alsways null.

typeof(IEnumerable<>)
typeof(IEnumerable)
typeof(Enumerable)

I can get method like that and it working.

var selectMethod = typeof(Enumerable).GetMethods()
            .Where(m => m.Name == nameof(Enumerable.Select))
            .First(m => m.GetParameters().Length == 2)
            .MakeGenericMethod(elementType, elementType);

But i interested why GetMethod not working in that case





Aucun commentaire:

Enregistrer un commentaire