1

I try to simulate List<T>.Any method via MethodInfo. First I define a class:

class Test
{
    int _value;
    public int Value { get; set; }

    public Test(int v)
    {
        _value = v;
    }
}

..and then I create a List<Test>

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

My aim to call list.Any(c=>c.Value>3) via MethodInfo, now I met the problem about how to locate the real method. I can find Any() method in System.Linq.Enumerable and System.Linq.Queryable.

When I check the definition about List<>, I think I should use the method in System.Linq.Enumerable, because it implements IEnumerable, is it right?

Now I have a new question if a class implements both IEnumerable and IQueryable, which MethodInfo should I use, such as Any(), Sum(), and so on.

user2155362
  • 1,657
  • 5
  • 18
  • 30
  • How is `List` and `List` in your code related? Furthermore you should use the `Enumerable` methods. – thehennyy Apr 09 '19 at 06:44
  • `IQueryable` inherits from `IEnumerable`. Usually you'll want to go from the more specific to the less specific. So use `System.Linq.Queryable` if you can, then fallback on `System.Linq.Enumerable` – Kevin Gosse Apr 09 '19 at 07:03
  • You are talking about `Any` as if its an instance method, but it's extension method ([msdn](https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.any)). Look [here](https://stackoverflow.com/a/15927145/1997232) how to invoke it. – Sinatr Apr 09 '19 at 07:08
  • Which `MethodInfo` to use depends on which method you want to call and why. – mm8 Apr 09 '19 at 12:27

0 Answers0