Added RedirectUsingNamedRoute
[castle.git] / Components / DictionaryAdapter / Castle.Components.DictionaryAdapter / Attributes / AttributesUtil.cs
blob2da346045343a1f9d55e910ec588fc6e977b3869
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.Components.DictionaryAdapter
17 using System;
18 using System.Collections.Generic;
19 using System.ComponentModel;
20 using System.Reflection;
22 /// <summary>
23 /// Helper class for retrieving attributes.
24 /// </summary>
25 public static class AttributesUtil
27 /// <summary>
28 /// Gets the type attribute.
29 /// </summary>
30 /// <param name="type">The type.</param>
31 /// <returns>The type attribute.</returns>
32 public static T GetTypeAttribute<T>(Type type) where T : class
34 T attribute = GetAttribute<T>(type);
36 if (attribute == null)
38 foreach (Type baseInterface in type.GetInterfaces())
40 attribute = GetTypeAttribute<T>(baseInterface);
41 if (attribute != null)
43 break;
48 return attribute;
51 /// <summary>
52 /// Gets the attribute.
53 /// </summary>
54 /// <param name="member">The member.</param>
55 /// <returns>The member attribute.</returns>
56 public static T GetAttribute<T>(MemberInfo member) where T : class
58 object[] attributes = member.GetCustomAttributes(typeof(T), false);
59 if (attributes.Length > 0)
61 return (T)attributes[0];
63 return null;
66 /// <summary>
67 /// Gets the type attributes.
68 /// </summary>
69 /// <param name="type">The type.</param>
70 /// <returns>The type attributes.</returns>
71 public static List<T> GetTypeAttributes<T>(Type type)
73 List<T> attributes = GetAttributes<T>(type);
75 if (attributes == null)
77 foreach (Type baseInterface in type.GetInterfaces())
79 attributes = GetTypeAttributes<T>(baseInterface);
80 if (attributes != null)
82 break;
87 return attributes;
90 /// <summary>
91 /// Gets the attributes.
92 /// </summary>
93 /// <param name="member">The member.</param>
94 /// <returns>The member attributes.</returns>
95 public static List<T> GetAttributes<T>(MemberInfo member)
97 List<T> attributes = null;
98 object[] custom = member.GetCustomAttributes(typeof(T), false);
100 if (custom.Length > 0)
102 attributes = new List<T>();
103 foreach (T builder in custom)
105 attributes.Add(builder);
109 return attributes;
112 /// <summary>
113 /// Gets the type converter.
114 /// </summary>
115 /// <param name="member">The member.</param>
116 /// <returns></returns>
117 public static Type GetTypeConverter(MemberInfo member)
119 TypeConverterAttribute attrib =
120 GetAttribute<TypeConverterAttribute>(member);
122 if (attrib != null)
124 return Type.GetType(attrib.ConverterTypeName);
127 return null;