mardi 9 avril 2019

How to invoke IEnumerable

I want to invoke IEnumerable<T>.Any() via MethodInfo.

    List<int> list = new List<int>();
    for (int i = 0; i < 10; i++)
    {
        list.Add(i);
    }

    Func<C, bool> func = c => c.Value > 10;
    Expression<Func<int, bool>> exp = (Expression<Func<int, bool>>)(c => c > 10);
    MethodInfo[] mis = typeof(System.Linq.Enumerable).GetMethods();
    MethodInfo miAny = mis.FirstOrDefault(d => d.Name == "Any" && d.GetParameters().Count()==2);
    var gf = miAny.MakeGenericMethod(new Type[] {list.GetType() });
    var re = gf.Invoke(null, new object[] {list,func });

But compiler report error at var re = gf.Invoke(null, new object[] {list,func });, say

`can't convert “System.Collections.Generic.List`1[System.Int32]” to “System.Collections.Generic.IEnumerable`1[System.Collections.Generic.List`1[System.Int32]]”`。

How can I repair this error?





Aucun commentaire:

Enregistrer un commentaire