0

我正在学习表达式树,所以请耐心等待。

这个想法是调用 intersect an 来接收数字 3。

我想我错过了什么。你能告诉我我在这里做错了什么吗?

    static void Main(string[] args)
    {
        List<int> arr1 = new List<int> { 1, 2, 3, 4, 5 };
        List<int> arr2 = new List<int> { 6, 7, 8, 9, 3 };

        var ex =
            Expression.Lambda<Func<List<int>>>(
                Expression.Call(
                    Expression.Constant(arr1), typeof(List<int>).GetMethod("Intersect"), Expression.Constant(arr2)));
     ....

为什么抛出该值不能为空?

4

1 回答 1

3

Intersect是扩展方法,所以typeof(List<int>).GetMethod("Intersect")返回 null
解决尝试从Enumerable

更新

获取相交试试这个

var intersectMethod = typeof(Enumerable).GetMethods().First(a => a.Name == "Intersect" && a.GetParameters().Count() == 2).MakeGenericMethod(typeof(int));

更好的

var intersectMethod = typeof(Enumerable).GetMember("Intersect").First().MakeGenericMethod(typeof(int));
于 2013-11-08T08:37:10.083 回答