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.