此代码在标记的行上引发异常:
using System;
using System.Linq.Expressions;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Action<int, int> a = (x, y) => Console.WriteLine(x + y);
ParameterExpression p1 = Expression.Parameter(typeof(int), "p1");
ParameterExpression p2 = Expression.Parameter(typeof(int), "p2");
// Here is the exception ArgumentNullException.
MethodCallExpression call = Expression.Call(a.Method, p1, p2);
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我已经在VS2013(就像魅力)和VS2015社区(抛出异常)中测试了这段代码.
我跟着.Net参考源,这导致我有一些代码条件,检查是否提供了方法IsStatic
.
在我的例子中,我传递的方法(a.Method
)在VS2013中是静态的,并且由于某种原因在VS2015中是非静态的(实例).如果没有,它会抛出,告诉我我没有提供Instance
论据.
为什么会这样?如何避免这种情况,以便Expression.Call
在新的Visual Studio中重新开始工作?