How to get MethodInfo for Enumerable.SequenceEqual

I am trying to get MethodInfofor Enumerable.SequenceEqualusing Type.GetMethod(...). So far I have tried the following:

var mi = typeof(Enumerable).GetMethod(nameof(Enumerable.SequenceEqual),
    BindingFlags.Static | BindingFlags.Public, null, CallingConventions.Any,
    new Type[] { typeof(IEnumerable<>), typeof(IEnumerable<>) }, null);

and

var enumTyped = typeof(IEnumerable<>).MakeGenericType(ValueType);
var mi = typeof(Enumerable).GetMethod(nameof(Enumerable.SequenceEqual),
    BindingFlags.Static | BindingFlags.Public, null, CallingConventions.Any,
    new Type[] { enumTyped, enumTyped }, null);

However, both solutions return nullinstead of the method I want. I know that a method can be restored by calling GetMethods()and filtering, but I would really like to know how to get it using GetMethod(...).

+4
source share
2 answers

Unfortunately, to get generic generic methods using Type.GetMethod(string name, Type[] types), you need to provide a method for the correct generic types in Type[], which means that when you try to do this:

Type requiredType = typeof(IEnumerable<>);
typeof(Enumerable).GetMethod("SequenceEqual", new Type[] { requiredType, requiredType });

You really needed to do something like this:

Type requiredType = typeof(IEnumerable<TSource>);
typeof(Enumerable).GetMethod("SequenceEqual", new Type[] { requiredType, requiredType });

SequenceEqual, IEnumerable<TSource> not IEnumerable<>.

public static IEnumerable<TSource> SequenceEqual<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second);

: TSource, . , IEnumerable<TSource> - , :

MethodInfo info = typeof(Enumerable)
    .GetMethods(BindingFlags.Static | BindingFlags.Public)
    .Where(x => x.Name.Contains("SequenceEqual"))
    .Single(x => x.GetParameters().Length == 2);
Type genericType = typeof(IEnumerable<>).MakeGenericType(infos.GetGenericArguments());

,

typeof(Enumerable).GetMethod("SequenceEqual", new Type[] { genericType, genericType });

SequenceEqual , , GetMethod GetMethods * ( CAN Binder GetMethod, , , , , ).

+3

. -, GetMethod, , . , GetMethods 180+ Enumerable, :

var mi = typeof(Enumerable).GetMember(nameof(Enumerable.SequenceEqual), MemberTypes.Method,
            BindingFlags.Static | BindingFlags.Public | BindingFlags.InvokeMethod).OfType<MethodInfo>().ToArray();

GetMember call 2 SequenceEqual, MakeGenericMethod, . , :

var source = Expression.Parameter(
            typeof(IEnumerable<string>), "source");
var target = Expression.Parameter(
            typeof(IEnumerable<string>), "target");
var callExp = Expression.Call(typeof(Enumerable), "SequenceEqual", new Type[] { typeof(string)},
            source, target);            
var lambda = Expression.Lambda<Func<IEnumerable<string>, IEnumerable<string>, bool>>(callExp, source, target).Compile();
var result = lambda(new[] { "1", "2", "3" }, new[] { "1", "2", "3" });
Debug.Assert(result);
+2

Source: https://habr.com/ru/post/1608938/


All Articles