1 namespace System.Web.Mvc {
3 using System.Collections.Generic;
5 using System.Reflection;
6 using System.Web.Mvc.Resources;
8 public class ReflectedControllerDescriptor : ControllerDescriptor {
10 private ActionDescriptor[] _canonicalActionsCache;
11 private readonly Type _controllerType;
12 private readonly ActionMethodSelector _selector;
14 public ReflectedControllerDescriptor(Type controllerType) {
15 if (controllerType == null) {
16 throw new ArgumentNullException("controllerType");
19 _controllerType = controllerType;
20 _selector = new ActionMethodSelector(_controllerType);
23 public sealed override Type ControllerType {
25 return _controllerType;
29 public override ActionDescriptor FindAction(ControllerContext controllerContext, string actionName) {
30 if (controllerContext == null) {
31 throw new ArgumentNullException("controllerContext");
33 if (String.IsNullOrEmpty(actionName)) {
34 throw new ArgumentException(MvcResources.Common_NullOrEmpty, "actionName");
37 MethodInfo matched = _selector.FindActionMethod(controllerContext, actionName);
38 if (matched == null) {
42 return new ReflectedActionDescriptor(matched, actionName, this);
45 private MethodInfo[] GetAllActionMethodsFromSelector() {
46 List<MethodInfo> allValidMethods = new List<MethodInfo>();
47 allValidMethods.AddRange(_selector.AliasedMethods);
48 allValidMethods.AddRange(_selector.NonAliasedMethods.SelectMany(g => g));
49 return allValidMethods.ToArray();
52 public override ActionDescriptor[] GetCanonicalActions() {
53 ActionDescriptor[] actions = LazilyFetchCanonicalActionsCollection();
55 // need to clone array so that user modifications aren't accidentally stored
56 return (ActionDescriptor[])actions.Clone();
59 public override object[] GetCustomAttributes(bool inherit) {
60 return ControllerType.GetCustomAttributes(inherit);
63 public override object[] GetCustomAttributes(Type attributeType, bool inherit) {
64 return ControllerType.GetCustomAttributes(attributeType, inherit);
67 internal override IEnumerable<FilterAttribute> GetFilterAttributes(bool useCache) {
68 if (useCache && GetType() == typeof(ReflectedControllerDescriptor)) {
69 // Do not look at cache in types derived from this type because they might incorrectly implement GetCustomAttributes
70 return ReflectedAttributeCache.GetTypeFilterAttributes(ControllerType);
72 return base.GetFilterAttributes(useCache);
75 public override bool IsDefined(Type attributeType, bool inherit) {
76 return ControllerType.IsDefined(attributeType, inherit);
79 private ActionDescriptor[] LazilyFetchCanonicalActionsCollection() {
80 return DescriptorUtil.LazilyFetchOrCreateDescriptors<MethodInfo, ActionDescriptor>(
81 ref _canonicalActionsCache /* cacheLocation */,
82 GetAllActionMethodsFromSelector /* initializer */,
83 methodInfo => ReflectedActionDescriptor.TryCreateDescriptor(methodInfo, methodInfo.Name, this) /* converter */);