2010-06-21 Marek Habersack <mhabersack@novell.com>
[mcs.git] / class / System.Web.Mvc / System.Web.Mvc / ReflectedControllerDescriptor.cs
blob990f1369e9214a1e4f55f354882aebf3a43464f7
1 /* ****************************************************************************
3 * Copyright (c) Microsoft Corporation. All rights reserved.
5 * This software is subject to the Microsoft Public License (Ms-PL).
6 * A copy of the license can be found in the license.htm file included
7 * in this distribution.
9 * You must not remove this notice, or any other, from this software.
11 * ***************************************************************************/
13 namespace System.Web.Mvc {
14 using System;
15 using System.Collections.Generic;
16 using System.Linq;
17 using System.Reflection;
18 using System.Web.Mvc.Resources;
20 public class ReflectedControllerDescriptor : ControllerDescriptor {
22 private ActionDescriptor[] _canonicalActionsCache;
23 private readonly Type _controllerType;
24 private readonly ActionMethodSelector _selector;
26 public ReflectedControllerDescriptor(Type controllerType) {
27 if (controllerType == null) {
28 throw new ArgumentNullException("controllerType");
31 _controllerType = controllerType;
32 _selector = new ActionMethodSelector(_controllerType);
35 public sealed override Type ControllerType {
36 get {
37 return _controllerType;
41 public override ActionDescriptor FindAction(ControllerContext controllerContext, string actionName) {
42 if (controllerContext == null) {
43 throw new ArgumentNullException("controllerContext");
45 if (String.IsNullOrEmpty(actionName)) {
46 throw new ArgumentException(MvcResources.Common_NullOrEmpty, "actionName");
49 MethodInfo matched = _selector.FindActionMethod(controllerContext, actionName);
50 if (matched == null) {
51 return null;
54 return new ReflectedActionDescriptor(matched, actionName, this);
57 private MethodInfo[] GetAllActionMethodsFromSelector() {
58 List<MethodInfo> allValidMethods = new List<MethodInfo>();
59 allValidMethods.AddRange(_selector.AliasedMethods);
60 allValidMethods.AddRange(_selector.NonAliasedMethods.SelectMany(g => g));
61 return allValidMethods.ToArray();
64 public override ActionDescriptor[] GetCanonicalActions() {
65 ActionDescriptor[] actions = LazilyFetchCanonicalActionsCollection();
67 // need to clone array so that user modifications aren't accidentally stored
68 return (ActionDescriptor[])actions.Clone();
71 public override object[] GetCustomAttributes(bool inherit) {
72 return ControllerType.GetCustomAttributes(inherit);
75 public override object[] GetCustomAttributes(Type attributeType, bool inherit) {
76 return ControllerType.GetCustomAttributes(attributeType, inherit);
79 public override bool IsDefined(Type attributeType, bool inherit) {
80 return ControllerType.IsDefined(attributeType, inherit);
83 private ActionDescriptor[] LazilyFetchCanonicalActionsCollection() {
84 return DescriptorUtil.LazilyFetchOrCreateDescriptors<MethodInfo, ActionDescriptor>(
85 ref _canonicalActionsCache /* cacheLocation */,
86 GetAllActionMethodsFromSelector /* initializer */,
87 methodInfo => ReflectedActionDescriptor.TryCreateDescriptor(methodInfo, methodInfo.Name, this) /* converter */);