我正在学习表达式树,我创建了一个方法,它接受一个
Expression<Func<bool>>
如果它满足一些条件,就执行它 - 请参见下面的代码。
private static void TryCommand(Expression<Func<bool>> expression)
{
var methodCallExpression = expression.Body as MethodCallExpression;
if (methodCallExpression == null)
{
throw new ArgumentException("expression must be a MethodCallExpression.");
}
if (methodCallExpression.Object.Type != typeof (MyClass))
{
throw new ArgumentException("expression must be operating on an instanceof MyClass.");
}
var func = expression.Compile();
var success = func.Invoke();
if(!success)
{
Console.WriteLine(methodCallExpression.Method.Name + "() failed with error code " + (func.Target as MyClass).GetError());
}
}
needs to be solved is how to improve the quality of the product. 需要解决的问题是如何提高产品的质量。
(func.Target as MyClass)
是空的。很明显我做错了什么!我如何访问该方法正在操作的实例?