Fix bugs in sizing TableLayoutPanel (Xamarin bug 18638)
[mono.git] / mcs / class / System.Web.Mvc / System.Web.Mvc / ReflectedControllerDescriptor.cs
1 /* ****************************************************************************\r
2  *\r
3  * Copyright (c) Microsoft Corporation. All rights reserved.\r
4  *\r
5  * This software is subject to the Microsoft Public License (Ms-PL). \r
6  * A copy of the license can be found in the license.htm file included \r
7  * in this distribution.\r
8  *\r
9  * You must not remove this notice, or any other, from this software.\r
10  *\r
11  * ***************************************************************************/\r
12 \r
13 namespace System.Web.Mvc {\r
14     using System;\r
15     using System.Collections.Generic;\r
16     using System.Linq;\r
17     using System.Reflection;\r
18     using System.Web.Mvc.Resources;\r
19 \r
20     public class ReflectedControllerDescriptor : ControllerDescriptor {\r
21 \r
22         private ActionDescriptor[] _canonicalActionsCache;\r
23         private readonly Type _controllerType;\r
24         private readonly ActionMethodSelector _selector;\r
25 \r
26         public ReflectedControllerDescriptor(Type controllerType) {\r
27             if (controllerType == null) {\r
28                 throw new ArgumentNullException("controllerType");\r
29             }\r
30 \r
31             _controllerType = controllerType;\r
32             _selector = new ActionMethodSelector(_controllerType);\r
33         }\r
34 \r
35         public sealed override Type ControllerType {\r
36             get {\r
37                 return _controllerType;\r
38             }\r
39         }\r
40 \r
41         public override ActionDescriptor FindAction(ControllerContext controllerContext, string actionName) {\r
42             if (controllerContext == null) {\r
43                 throw new ArgumentNullException("controllerContext");\r
44             }\r
45             if (String.IsNullOrEmpty(actionName)) {\r
46                 throw new ArgumentException(MvcResources.Common_NullOrEmpty, "actionName");\r
47             }\r
48 \r
49             MethodInfo matched = _selector.FindActionMethod(controllerContext, actionName);\r
50             if (matched == null) {\r
51                 return null;\r
52             }\r
53 \r
54             return new ReflectedActionDescriptor(matched, actionName, this);\r
55         }\r
56 \r
57         private MethodInfo[] GetAllActionMethodsFromSelector() {\r
58             List<MethodInfo> allValidMethods = new List<MethodInfo>();\r
59             allValidMethods.AddRange(_selector.AliasedMethods);\r
60             allValidMethods.AddRange(_selector.NonAliasedMethods.SelectMany(g => g));\r
61             return allValidMethods.ToArray();\r
62         }\r
63 \r
64         public override ActionDescriptor[] GetCanonicalActions() {\r
65             ActionDescriptor[] actions = LazilyFetchCanonicalActionsCollection();\r
66 \r
67             // need to clone array so that user modifications aren't accidentally stored\r
68             return (ActionDescriptor[])actions.Clone();\r
69         }\r
70 \r
71         public override object[] GetCustomAttributes(bool inherit) {\r
72             return ControllerType.GetCustomAttributes(inherit);\r
73         }\r
74 \r
75         public override object[] GetCustomAttributes(Type attributeType, bool inherit) {\r
76             return ControllerType.GetCustomAttributes(attributeType, inherit);\r
77         }\r
78 \r
79         public override bool IsDefined(Type attributeType, bool inherit) {\r
80             return ControllerType.IsDefined(attributeType, inherit);\r
81         }\r
82 \r
83         private ActionDescriptor[] LazilyFetchCanonicalActionsCollection() {\r
84             return DescriptorUtil.LazilyFetchOrCreateDescriptors<MethodInfo, ActionDescriptor>(\r
85                 ref _canonicalActionsCache /* cacheLocation */,\r
86                 GetAllActionMethodsFromSelector /* initializer */,\r
87                 methodInfo => ReflectedActionDescriptor.TryCreateDescriptor(methodInfo, methodInfo.Name, this) /* converter */);\r
88         }\r
89 \r
90     }\r
91 }\r