3

I've seen users tackle this question before, like in thread: C# very dynamic invocation

But I haven't had much success with the code given. I'm basically trying to call a method using some variable string.

Here's what I have so far.

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Model : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

       string TargetMethod = "Index";
       Type t = System.Type.GetType("_Model");

       System.Reflection.MethodInfo methodInfo = t.GetMethod(TargetMethod);
       methodInfo.Invoke(t, new object[] {});

    }

     protected void Index(){
       Response.Write("Index: Dynamically called!"); 
     }

}

On the view page, I should see the string "Index: Dynamically called!" written to the screen but I'm getting error:

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Please help, thanks.

0

1 Answer 1

3

By default, GetMethod only returns public methods; add BindingFlags.NonPublic | BindingFlags.Instance and it should work.

var methodInfo = t.GetMethod(TargetMethod,
      BindingFlags.NonPublic | BindingFlags.Instance);

or to work for both public and non-public:

var methodInfo = t.GetMethod(TargetMethod,
      BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);

Also: it is an instance method, and you want to call it on the current instance, so:

methodInfo.Invoke(this, new object[] {});

Note the this, not t.

Sign up to request clarification or add additional context in comments.

6 Comments

Still won't work because the arguments to Invoke are wrong. Instead of t, it should be this.
Also there is another error in his code. Should be methodInfo.Invoke(this, new object[] {});
I added: System.Reflection.MethodInfo methodInfo = t.GetMethod(TargetMethod,BindingFlags.NonPublic | BindingFlags.Instance); but now I get error: CS0103: The name 'BindingFlags' does not exist in the current context
@Harvester316 so: resolve it like you would any other type. In this case, that is System.Reflection, so either add using System.Reflection; at the top, or explicitly use System.Reflection.BindingFlags. Also: just pressing ctrl+. will fix this for you automatically (giving you a choice of which approach to take)
Thanks a lot Marc. It's alive and kicking. New to asp.net. I use notepad++, so ctrl+ won't work for me. By the way, not sure why my question got down rated. I guess I wasn't clear or too dumb for some people to be kind. Yikes. Thanks Marc!!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.