1 /* ****************************************************************************
\r
3 * Copyright (c) Microsoft Corporation. All rights reserved.
\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
9 * You must not remove this notice, or any other, from this software.
\r
11 * ***************************************************************************/
\r
13 namespace System.Web.Mvc {
\r
15 using System.Collections.Generic;
\r
17 using System.Reflection;
\r
18 using System.Web.Mvc.Resources;
\r
20 public class ReflectedControllerDescriptor : ControllerDescriptor {
\r
22 private ActionDescriptor[] _canonicalActionsCache;
\r
23 private readonly Type _controllerType;
\r
24 private readonly ActionMethodSelector _selector;
\r
26 public ReflectedControllerDescriptor(Type controllerType) {
\r
27 if (controllerType == null) {
\r
28 throw new ArgumentNullException("controllerType");
\r
31 _controllerType = controllerType;
\r
32 _selector = new ActionMethodSelector(_controllerType);
\r
35 public sealed override Type ControllerType {
\r
37 return _controllerType;
\r
41 public override ActionDescriptor FindAction(ControllerContext controllerContext, string actionName) {
\r
42 if (controllerContext == null) {
\r
43 throw new ArgumentNullException("controllerContext");
\r
45 if (String.IsNullOrEmpty(actionName)) {
\r
46 throw new ArgumentException(MvcResources.Common_NullOrEmpty, "actionName");
\r
49 MethodInfo matched = _selector.FindActionMethod(controllerContext, actionName);
\r
50 if (matched == null) {
\r
54 return new ReflectedActionDescriptor(matched, actionName, this);
\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
64 public override ActionDescriptor[] GetCanonicalActions() {
\r
65 ActionDescriptor[] actions = LazilyFetchCanonicalActionsCollection();
\r
67 // need to clone array so that user modifications aren't accidentally stored
\r
68 return (ActionDescriptor[])actions.Clone();
\r
71 public override object[] GetCustomAttributes(bool inherit) {
\r
72 return ControllerType.GetCustomAttributes(inherit);
\r
75 public override object[] GetCustomAttributes(Type attributeType, bool inherit) {
\r
76 return ControllerType.GetCustomAttributes(attributeType, inherit);
\r
79 public override bool IsDefined(Type attributeType, bool inherit) {
\r
80 return ControllerType.IsDefined(attributeType, inherit);
\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