I have viewed similar SO questions but cannot figure out why mine won't work.
I need to convert my Func<string, bool> value to an Expression to be used in the Moq framework but I cannot get passed an error when trying to convert the Func to an Expression.
This is the error:
Static method requires null instance, non-static method requires non-null instance.
This is my sample code:
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")));
}
}
}
Not sure if my function is a static or non-static, but I would have thought it was static. I inspected the Func object using the debugger and there is a field called "IsStatic" set to false (value.Method.IsStatic). A bit confused what else to try.
Thank you.
Stack Trace:
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


MyMethod, etc.It.Iscall? Is it inExpression.Lambda? Is it in_myMock.Setup? Again, a minimal reproducible example would really help.