Expression.Call导致“静态方法需要空实例,非静态方法需要非空实例”

时间:2019-09-03 08:36:07

标签: c# lambda

我已经看过类似的SO问题,但无法弄清楚为什么我的问题不起作用。 我需要将Func<string, bool>的值转换为要在Moq框架中使用的表达式,但是在尝试将Func转换为表达式时无法传递错误。

这是错误:

  

静态方法需要空实例,非静态方法需要   非空实例。

这是我的示例代码:

using System;
using System.Linq.Expressions;

namespace ConsoleApp1
{
    class Program
    {
        public class MyObject
        {
            public void Add<T>(Func<T, bool> value)
            {
                // Line below causes error: Static method requires null instance, 
                // non-static method requires non-null instance.
                Expression<Func<T, bool>> expression =
                    Expression.Lambda<Func<T, bool>>(Expression.Call(value.Method));


                // I need to use the expression for the line below that is commented out
                // (for context reasons I have included this)
                //_myMock.Setup(m => m.MyMethod(key, It.Is(expression))).Returns("test");
            }
        }

        public static void Main(string[] args)
        {
            // Call it using:
            var myObject = new MyObject();
            myObject.Add(new Func<string, bool>(x => x.StartsWith("test")));
        }
    }
}

不确定我的函数是静态的还是非静态的,但是我会认为它是静态的。我使用调试器检查了Func对象,并且有一个名为“ IsStatic”的字段设置为false(value.Method.IsStatic)。有点困惑,还有什么可以尝试的。

谢谢。

堆栈跟踪:

System.ArgumentException
  HResult=0x80070057
  Message=Static method requires null instance, non-static method requires non-null instance.
Parameter name: method
  Source=System.Core
  StackTrace:
   at System.Linq.Expressions.Expression.ValidateStaticOrInstanceMethod(Expression instance, MethodInfo method)
   at System.Linq.Expressions.Expression.Call(Expression instance, MethodInfo method, IEnumerable`1 arguments)
   at System.Linq.Expressions.Expression.Call(MethodInfo method, Expression[] arguments)
   at ConsoleApp1.Program.MyObject.Add[T](Func`2 value) in C:\Users\userName\source\repos\ConsoleApp1\ConsoleApp1\Program.cs:line 14
   at ConsoleApp1.Program.Main(String[] args) in C:\Users\userName\source\repos\ConsoleApp1\ConsoleApp1\Program.cs:line 28

1 个答案:

答案 0 :(得分:2)

您要在表达式调用中包装的方法不是静态的。

enter image description here

要调用非静态方法,您将需要传递有效的实例,这样,如果您实际上从func外部使用某些变量,则可能会变得棘手。您可以进一步检查该方法并查看其声明类型。使Expression.Call工作需要它的实例。


要完成模拟设置,这是您的目标,因此您可以修改Add方法并直接获取表达式

Add<T>(Expression<Func<T, bool>> expression)
{
    _myMock.Setup(m => m.MyMethod(key, It.Is(expression))).Returns("test");
}

这将仅在按以下方式调用时起作用:

myObject.Add((string x) => x.StartsWith("test"));

enter image description here