0

I am trying to invoke a Generic method through reflection but it throw an exception Non-Static mehod requires a target

here is the method I am using to invoke the generic method

public  object ExecuteParseTree(Type arg,ParameterExpression param, JsonElement el)
{
            Type arg_type = arg;
            Type class_type = typeof(JsonExpressionParser<FunctionRule>);
            MethodInfo mi = class_type.GetMethod("ParseTree");
            MethodInfo mi2 = mi.MakeGenericMethod(new Type[] { arg_type });
            return mi2.Invoke(null, new object[] {  el, param });
}

the method I am calling

  public Expression ParseTree<T>(
            JsonElement condition,
            ParameterExpression parm)
{
}

what do I need to call this method without making it static?

changing the code to

cs

  public  object ExecuteParseTree(Type arg,ParameterExpression param, JsonElement el)
        {
            Type arg_type = arg;
            Type class_type = typeof(JsonExpressionParser<FunctionRule>);
            MethodInfo mi = class_type.GetMethod("ParseTree");
            MethodInfo mi2 = mi.MakeGenericMethod(new Type[] { arg_type });
            var instance = (JsonExpressionParser<FunctionRule>)Activator.CreateInstance(class_type, args: new object[] { Query_Type, arg });
            instance.SetQueryType(el);
            return mi2.Invoke(instance, new object[] {  el, param });
        }

throws System.MissingMethodException: 'Constructor on type 'ExpressionGenerator.JsonExpressionParser`1[[ExpressionGenerator.FunctionRule, ExpressionGenerator on Activator.CreateInstance

and if i used this instead an instance it returns that the target does not match(different fields I supose?)

PontiacGTX
  • 185
  • 2
  • 15

0 Answers0