I have found following code to get all action from controller name.
Type t = typeof(YourControllerType);
MethodInfo[] mi = t.GetMethods();
foreach (MethodInfo m in mi)
{
if (m.IsPublic)
if (typeof(ActionResult).IsAssignableFrom(m.ReturnParameter.ParameterType))
methods = m.Name + Environment.NewLine + methods;
}
I want to make following code dynamic as function by passing controller name as follows:
public string get_all_action(type ob)
{
string methods = "";
Type t = typeof(ob);
MethodInfo[] mi = t.GetMethods();
foreach (MethodInfo m in mi)
{
if (m.IsPublic)
if (typeof(ActionResult).IsAssignableFrom(m.ReturnParameter.ParameterType))
methods = m.Name + Environment.NewLine + methods;
}
return methods;
}
But I am confuse what type of parameter should passed to the function at definition and while calling the function.
I have following code to get all controller and pass controller name to get_all_action().
var asm = Assembly.GetExecutingAssembly();
var controllerTypes = from d in asm.GetExportedTypes() where typeof(IController).IsAssignableFrom(d) select d;
foreach(var val in controllerTypes)
{
string actionname = get_all_action(val.Name);
}
Here, val.Name is in string type therefore it throws exception:
Object reference not set to an instance of an object.
Your
get_all_actionmethod is expecting to receive a Type as the input parameter, whereas you’re attempting to call it with a String (i.e. the Type name) as the input parameter.The line here:
should instead be:
And the first few lines of your
get_all_actionmethod should be changed to be: