[corlib] Remove unused files
[mono.git] / mcs / class / System.Web.Mvc3 / Mvc / ReflectedControllerDescriptor.cs
1 namespace System.Web.Mvc {
2     using System;
3     using System.Collections.Generic;
4     using System.Linq;
5     using System.Reflection;
6     using System.Web.Mvc.Resources;
7
8     public class ReflectedControllerDescriptor : ControllerDescriptor {
9
10         private ActionDescriptor[] _canonicalActionsCache;
11         private readonly Type _controllerType;
12         private readonly ActionMethodSelector _selector;
13
14         public ReflectedControllerDescriptor(Type controllerType) {
15             if (controllerType == null) {
16                 throw new ArgumentNullException("controllerType");
17             }
18
19             _controllerType = controllerType;
20             _selector = new ActionMethodSelector(_controllerType);
21         }
22
23         public sealed override Type ControllerType {
24             get {
25                 return _controllerType;
26             }
27         }
28
29         public override ActionDescriptor FindAction(ControllerContext controllerContext, string actionName) {
30             if (controllerContext == null) {
31                 throw new ArgumentNullException("controllerContext");
32             }
33             if (String.IsNullOrEmpty(actionName)) {
34                 throw new ArgumentException(MvcResources.Common_NullOrEmpty, "actionName");
35             }
36
37             MethodInfo matched = _selector.FindActionMethod(controllerContext, actionName);
38             if (matched == null) {
39                 return null;
40             }
41
42             return new ReflectedActionDescriptor(matched, actionName, this);
43         }
44
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();
50         }
51
52         public override ActionDescriptor[] GetCanonicalActions() {
53             ActionDescriptor[] actions = LazilyFetchCanonicalActionsCollection();
54
55             // need to clone array so that user modifications aren't accidentally stored
56             return (ActionDescriptor[])actions.Clone();
57         }
58
59         public override object[] GetCustomAttributes(bool inherit) {
60             return ControllerType.GetCustomAttributes(inherit);
61         }
62
63         public override object[] GetCustomAttributes(Type attributeType, bool inherit) {
64             return ControllerType.GetCustomAttributes(attributeType, inherit);
65         }
66
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);
71             }
72             return base.GetFilterAttributes(useCache);
73         }
74
75         public override bool IsDefined(Type attributeType, bool inherit) {
76             return ControllerType.IsDefined(attributeType, inherit);
77         }
78
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 */);
84         }
85
86     }
87 }