1 // Copyright 2004-2008 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
.ComponentModel
;
20 using System
.Reflection
;
23 /// Helper class for retrieving attributes.
25 public static class AttributesUtil
28 /// Gets the type attribute.
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)
52 /// Gets the attribute.
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];
67 /// Gets the type attributes.
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)
91 /// Gets the attributes.
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
);
113 /// Gets the type converter.
115 /// <param name="member">The member.</param>
116 /// <returns></returns>
117 public static Type
GetTypeConverter(MemberInfo member
)
119 TypeConverterAttribute attrib
=
120 GetAttribute
<TypeConverterAttribute
>(member
);
124 return Type
.GetType(attrib
.ConverterTypeName
);