Snippet MVC .NET C#: Liste des contrôleurs et actions par réflexion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web.Mvc;
using System.Web.Script.Serialization;
namespace Tools.ControllerLister
{
public class ControllerLister
{
///
///
///
///
private static List
{
return Assembly.GetCallingAssembly().GetTypes().Where(type => type.IsSubclassOf(typeof(T))).ToList();
}
///
///
///
public static List
{
List
GetSubClasses
return controllerNames;
}
///
///
/// Le nom du controleur
///
public List
{
var types =
from a in AppDomain.CurrentDomain.GetAssemblies()
from t in a.GetTypes()
where typeof(IController).IsAssignableFrom(t) &&
string.Equals(controllerName, t.Name, StringComparison.OrdinalIgnoreCase)
select t;
var controllerType = types.FirstOrDefault();
if (controllerType == null)
{
return Enumerable.Empty
}
return new ReflectedControllerDescriptor(controllerType)
.GetCanonicalActions().Select(x => x.ActionName).Distinct()
.ToList();
}
///
///
///
public Dictionary
{
Dictionary
List
foreach (string control in controls)
{
dicoControllers.Add(control, GetActionNamesList(control));
}
return dicoControllers;
}
public SelectList GetList()
{
List
/**** create parent_id dropdown list with opt-group ****/
List
foreach (string aControllerName in lstControllerItem)
{
bool selected = false;
SelectListItem selectListItem = new SelectListItem() { Value = aControllerName, Text = aControllerName, Selected = selected };
list_controllers.Add(selectListItem);
}
SelectList select = new SelectList(lstControllerItem,”id”,”name”);
return select;
}
///
///
///
public string GetJsonString()
{
Dictionary
var jss = new JavaScriptSerializer();
var json = jss.Serialize(dict);
return json;
}
}