I have following function to return the selectlist of the action name from controller name of string type:
public ActionResult get_all_action(string controllername)
{
Type t = Type.GetType(controllername);
MethodInfo[] mi = t.GetMethods();
List<SelectListItem> action = new List<SelectListItem>();
foreach (MethodInfo m in mi)
{
if (m.IsPublic)
if (typeof(ActionResult).IsAssignableFrom(m.ReturnParameter.ParameterType))
{
action.Add(new SelectListItem() { Value = m.Name, Text = m.Name });
}
}
var List = new SelectList(action, "Value", "Text");
return Json(List, JsonRequestBehavior.AllowGet);
}
The parameter controllername of get_all_action() is passed as for example “AccountController”. But the exception is thrown at
MethodInfo[] mi = t.GetMethods();
as:
Object reference not set to an instance of an object.
"AccountController"is not a full type name; it needs to be something like"YourApp.Whatever.AccountController"forGetType()to find it. It is also worth making it explicit which assembly it is in, for example:(which assumes we mean the same assembly / namespace)