Create a PropertyDescriptor class to collection property information and simplify...
[castle.git] / Components / DictionaryAdapter / Castle.Components.DictionaryAdapter / Attributes / AttributesUtil.cs
blob6531425bb3b19ec28ff71eddbf266d7c73919e7d
1 // Copyright 2004-2007 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.Reflection;
21 /// <summary>
22 /// Helper class for retrieving attributes.
23 /// </summary>
24 public static class AttributesUtil
26 /// <summary>
27 /// Gets the type attribute.
28 /// </summary>
29 /// <param name="type">The type.</param>
30 /// <returns>The type attribute.</returns>
31 public static T GetTypeAttribute<T>(Type type) where T : class
33 T attribute = GetAttribute<T>(type);
35 if (attribute == null)
37 foreach (Type baseInterface in type.GetInterfaces())
39 attribute = GetTypeAttribute<T>(baseInterface);
40 if (attribute != null)
42 break;
47 return attribute;
50 /// <summary>
51 /// Gets the attribute.
52 /// </summary>
53 /// <param name="member">The member.</param>
54 /// <returns>The member attribute.</returns>
55 public static T GetAttribute<T>(MemberInfo member) where T : class
57 object[] attributes = member.GetCustomAttributes(typeof(T), false);
58 if (attributes.Length > 0)
60 return (T)attributes[0];
62 return null;
65 /// <summary>
66 /// Gets the type attributes.
67 /// </summary>
68 /// <param name="type">The type.</param>
69 /// <returns>The type attributes.</returns>
70 public static List<T> GetTypeAttributes<T>(Type type)
72 List<T> attributes = GetAttributes<T>(type);
74 if (attributes == null)
76 foreach (Type baseInterface in type.GetInterfaces())
78 attributes = GetTypeAttributes<T>(baseInterface);
79 if (attributes != null)
81 break;
86 return attributes;
89 /// <summary>
90 /// Gets the attributes.
91 /// </summary>
92 /// <param name="member">The member.</param>
93 /// <returns>The member attributes.</returns>
94 public static List<T> GetAttributes<T>(MemberInfo member)
96 List<T> attributes = null;
97 object[] custom = member.GetCustomAttributes(typeof(T), false);
99 if (custom.Length > 0)
101 attributes = new List<T>();
102 foreach (T builder in custom)
104 attributes.Add(builder);
108 return attributes;