c# - Expressions static method requires null instance non-static method requires non-null instance -


i'm new using expressions , getting following error:

system.argumentexception : static method requires null instance, non-static method requires non-null instance.
parameter name: method

the code follows:

        int inp = 100;         object inparam = inp;         type inparamtype = inparam.gettype();          parameterexpression pe = expression.parameter(typeof(s), "pe");          expression left = expression.property(pe, typeof(s).getproperty(propname));         expression right = expression.constant(inparam, inparamtype);          methodinfo mi = inparamtype.getmethod(operand, bindingflags.instance | bindingflags.public, null, new type[] { typeof(object) }, null);         expression e1 = expression.call(mi, left, right); 

you're using bindingflags.instance, you'll instance methods. instance methods must called c# a.f(b), not f(a, b), , translates expression tree expression.call(left, mi, right), not expression.call(mi, left, right). that's exception telling you:

static method requires null instance, non-static method requires non-null instance.

in case have non-static method, therefore must pass in instance on call method.


Comments