I have the following code:
public ActionResult Test()
{
Assembly asm = Assembly.GetExecutingAssembly();
string name;
string text = "Controller";
foreach (var item in asm.GetTypes()
.Where(type => typeof(Controller).IsAssignableFrom(type)) //filter controllers
.SelectMany(type => type.GetMethods())
.Where(method => method.IsPublic && !method.IsDefined(typeof(NonActionAttribute))))
{
name = item.DeclaringType.Name;
//check if the word in text is in the end of the controller name:
if (name.LastIndexOf(text) > 0 && name.LastIndexOf(text) + text.Length == name.Length)
{
System.Diagnostics.Debug.WriteLine(item.Name +" / " + item.DeclaringType.Name);
if (item.ReturnParameter.GetType() == typeof(ActionResult) || item.ReturnParameter.GetType() == typeof(JsonResult))
{
System.Diagnostics.Debug.WriteLine("YES--> "+item.Name + " / " + item.DeclaringType.Name);
}
}
}
return View();
}
This suppose to bring me all the Controllers which ends up with the string "Controller" (such as "HelpController"), and then iterate their public methods.
This works fine but brings me many properties which I don't want. I only want methods that return "ActionResult" or "JsonResult".
The problem is in the if (item.ReturnParameter.GetType()==...) , I see in debug mode that the return type is ActionResult, yet the condition is false... I don't understand where is the problem.
Aucun commentaire:
Enregistrer un commentaire