-3

I want to invoke a class "***" is the solution that works for me but I want to invoke THIS IS THE SOLUTION THAT GIVES ME THE ERROR :

Type t = Type.GetType(svClass);
MethodInfo method = t.GetMethod("execute", BindingFlags.instance| BindingFlags.Public);

Ret = (string)method.Invoke(null, new object[] { context.Request});
    public string execute(HttpRequest req)

so that I tried to MethodInfo method = t.GetMethod("execute", BindingFlags.instance | BindingFlags.Public);

but it gives me the error "non-static method requires a target"

*** THIS IS THE WORKING SOLUTION FOR STATIC METHOD

Type t = Type.GetType(svClass);
MethodInfo method = t.GetMethod("execute", BindingFlags.static| BindingFlags.Public);

Ret = (string)method.Invoke(null, new object[] { context.Request});

to invoke

public class XXXXX
    {
        public static string execute(HttpRequest req){}
    }
lol
  • 93
  • 3
  • 11
  • 6
    You need to invoke a non-static method on a instance. – Simon Karlsson Jan 27 '16 at 09:22
  • But he does from what I see, the problem would be with binding flags. – Jerry Switalski Jan 27 '16 at 09:28
  • yes so I tries public string execute(HttpRequest req){} and BindingFlags.instance – lol Jan 27 '16 at 09:29
  • but gives me the error – lol Jan 27 '16 at 09:29
  • 1
    Um, the `execute` method you've shown is a static method... and your "working solution for static method" is incorrect, as it passes `this` as the first argument when it should logically pass `null` (as there's no target). It also wouldn't compile at the moment, as `BindingFlags.static` should be `BindingFlags.Static`, so this clearly isn't your real code. Please provide a [mcve] demonstrating the problem. – Jon Skeet Jan 27 '16 at 09:29
  • To call an instance method, you need an instance of the class. So [create one](https://msdn.microsoft.com/en-us/library/system.activator.createinstance.aspx) – Hans Kesting Jan 27 '16 at 09:31
  • @JonSkeet it is static , and with the static execute and BindingFlags.static it works but I want my method to be like public string execute(HttpRequest req){} in this case I should change BindingFlags.static to BindingFlags.instance but gives me the error – lol Jan 27 '16 at 09:31
  • You are invoking from a static method, so as @skeet stated you need to call invoke with an instance of your class, no with "this". Ie "var instance=new YourClass" and then "Invoke(instance,..)" – tede24 Jan 27 '16 at 09:35
  • in this solution I do not know the name of the class, so i am reading it like string sClass = "SX.Services." + Par.service; so how can make new if I do not know what is the name? – lol Jan 27 '16 at 09:47
  • 1
    Again, please include a [mcve]. Rather than showing code that *does* work, show the code that *doesn't*... in a way that we can reproduce the issue. – Jon Skeet Jan 27 '16 at 09:55
  • @JonSkeet I have already wrote the one that does not at the beginning of the question!! all those written before "*** THIS IS THE WORKING SOLUTION FOR STATIC METHOD" are the code that does not work – lol Jan 27 '16 at 11:34
  • 1
    No, you haven't. You've written a method signature, but *not* a [mcve]. You haven't shown how you called `Invoke`, for example. You haven't given us something we can copy, compile and run to see the problem. – Jon Skeet Jan 27 '16 at 11:45
  • @JonSkeet I updated with the solution that is not working – lol Jan 27 '16 at 14:00
  • 2
    That's *still* not a [mcve]. Ask yourself whether someone else can select some text, copy it, paste it into a new empty file, compile that file and run the code, to see the problem you're facing. If the answer is "no", it's incomplete. Next ask yourself whether everything within that is required in order to demonstrate the problem. If the answer is "no", it's not minimal. – Jon Skeet Jan 27 '16 at 14:42

1 Answers1

3

The secret is to change your binding flags to get a MethodInfo that matches the signature of the method you wish to call.

Eg:

 public static string execute(HttpRequest req){}

Will be accessed via

MethodInfo method = t.GetMethod("execute", BindingFlags.static| BindingFlags.Public);

However, to access

public string execute(HttpRequest req){}

you need to do

var classObj = new Class();
MethodInfo method = classObj.GetType().GetMethod("execute", BindingFlags.Instance| BindingFlags.Public);

Instance means that the method is a member of a class object, and not of the class type. (Instance vs Static)

var parameterArray = new object[]{ YourHttpRequestHere};
var result = method.Invoke(classObj,parameterArray);

So remember, if the method belongs to an instance, then you need to grab the method from that instance type, and then you need to invoke it with a reference to the instance variable (classObj) above.

Baaleos
  • 1,703
  • 12
  • 22
  • Thanks, the problem is that the class name is passed as an input parameters so for this I read it from input in a string 'xvClass' and then Type t = Type.GetType(svClass); and --> MethodInfo method = t.GetMethod("execute", BindingFlags.static| BindingFlags.Public); To make an object of my class I do not know how I can do it with the name of the class in a string – lol Jan 27 '16 at 13:55