1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
18 using System
.Collections
.Generic
;
19 using System
.Reflection
;
22 /// Helper class for retrieving attributes.
24 public static class AttributesUtil
27 /// Gets the type attribute.
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)
51 /// Gets the attribute.
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];
66 /// Gets the type attributes.
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)
90 /// Gets the attributes.
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
);