You can solve your problem using the following set of classes.
First, we need to create the Contains class, which will determine which elements will be selected from the original array.
class Contains { public bool Value { get; set; } public Contains(object[] items, object item) { Value = (bool)(typeof(Enumerable).GetMethods() .Where(x => x.Name.Contains("Contains")) .First() .MakeGenericMethod(typeof(object)) .Invoke(items, new object[] { items, item })); } }
Then we need to create the Where class, which will be used to generate the predicate based on which elements will be selected. It should be clear that in our case we will use the Contains class for our predicate method.
class Where { public object Value { get; set; } public Where(object[] items, object[] items2) { Value = typeof(Enumerable).GetMethods() .Where(x => x.Name.Contains("Where")) .First() .MakeGenericMethod(typeof(object)) .Invoke(items2, new object[] { items2, new Func<object, bool>(i => new Contains(items, i).Value) }); } }
The last step is to simply call the result obtained from the Where class, which is actually of type Enumerable.WhereArrayIterator, and not of type List, because the result of the Where Extension method is the result of deferred execution.
Thus, we need to create an object not deferred by calling its ToList extension method and get our result.
class ToList { public List<object> Value { get; set; } public ToList(object[] items, object[] items2) { var where = new Where(items, items2).Value; Value = (typeof(Enumerable).GetMethods() .Where(x => x.Name.Contains("ToList")) .First() .MakeGenericMethod(typeof(object)) .Invoke(where, new object[] { where })) as List<object>; } }
In the end, you can simply test the whole process using the following class.
class Program { static void Main() { var items = new object[] { 1, 2, 3, 4 }; var items2 = new object[] { 2, 3, 4, 5 }; new ToList(items, items2).Value.ForEach(x => Console.WriteLine(x)); Console.Read(); } }