2 // CustomAttributeExtensions.cs
5 // Marek Safar <marek.safar@gmail.com>
7 // Copyright (C) 2012 Xamarin, Inc (http://www.xamarin.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Collections.Generic;
34 namespace System.Reflection
36 public static class CustomAttributeExtensions
38 public static T GetCustomAttribute<T> (this Assembly element) where T : Attribute
40 return (T) Attribute.GetCustomAttribute (element, typeof (T));
43 public static T GetCustomAttribute<T> (this MemberInfo element) where T : Attribute
45 return (T) Attribute.GetCustomAttribute (element, typeof (T));
48 public static T GetCustomAttribute<T> (this Module element) where T : Attribute
50 return (T) Attribute.GetCustomAttribute (element, typeof (T));
53 public static T GetCustomAttribute<T> (this ParameterInfo element) where T : Attribute
55 return (T) Attribute.GetCustomAttribute (element, typeof (T));
58 public static T GetCustomAttribute<T> (this MemberInfo element, bool inherit) where T : Attribute
60 return (T) Attribute.GetCustomAttribute (element, typeof (T), inherit);
63 public static T GetCustomAttribute<T> (this ParameterInfo element, bool inherit) where T : Attribute
65 return (T) Attribute.GetCustomAttribute (element, typeof (T), inherit);
68 public static Attribute GetCustomAttribute (this Assembly element, Type attributeType)
70 return Attribute.GetCustomAttribute (element, attributeType);
73 public static Attribute GetCustomAttribute (this MemberInfo element, Type attributeType)
75 return Attribute.GetCustomAttribute (element, attributeType);
78 public static Attribute GetCustomAttribute (this MemberInfo element, Type attributeType, bool inherit)
80 return Attribute.GetCustomAttribute (element, attributeType, inherit);
83 public static Attribute GetCustomAttribute (this Module element, Type attributeType)
85 return Attribute.GetCustomAttribute (element, attributeType);
88 public static Attribute GetCustomAttribute (this ParameterInfo element, Type attributeType)
90 return Attribute.GetCustomAttribute (element, attributeType);
93 public static Attribute GetCustomAttribute (this ParameterInfo element, Type attributeType, bool inherit)
95 return Attribute.GetCustomAttribute (element, attributeType, inherit);
98 public static IEnumerable<Attribute> GetCustomAttributes (this Assembly element)
100 return Attribute.GetCustomAttributes (element);
103 public static IEnumerable<Attribute> GetCustomAttributes (this Assembly element, Type attributeType)
105 return Attribute.GetCustomAttributes (element, attributeType);
108 public static IEnumerable<Attribute> GetCustomAttributes (this Module element)
110 return Attribute.GetCustomAttributes (element);
113 public static IEnumerable<Attribute> GetCustomAttributes (this Module element, Type attributeType)
115 return Attribute.GetCustomAttributes (element);
118 public static IEnumerable<Attribute> GetCustomAttributes (this ParameterInfo element)
120 return Attribute.GetCustomAttributes (element);
123 public static IEnumerable<Attribute> GetCustomAttributes (this ParameterInfo element, bool inherit)
125 return Attribute.GetCustomAttributes (element, inherit);
128 public static IEnumerable<Attribute> GetCustomAttributes (this ParameterInfo element, Type attributeType)
130 return Attribute.GetCustomAttributes (element, attributeType);
133 public static IEnumerable<Attribute> GetCustomAttributes (this ParameterInfo element, Type attributeType,
136 return Attribute.GetCustomAttributes (element, attributeType, inherit);
139 public static IEnumerable<Attribute> GetCustomAttributes (this MemberInfo element)
141 return Attribute.GetCustomAttributes (element);
144 public static IEnumerable<Attribute> GetCustomAttributes (this MemberInfo element, bool inherit)
146 return Attribute.GetCustomAttributes (element, inherit);
149 public static IEnumerable<Attribute> GetCustomAttributes (this MemberInfo element, Type attributeType)
151 return Attribute.GetCustomAttributes (element, attributeType);
154 public static IEnumerable<Attribute> GetCustomAttributes (this MemberInfo element, Type attributeType,
157 return Attribute.GetCustomAttributes (element, attributeType, inherit);
160 public static IEnumerable<T> GetCustomAttributes<T> (this Assembly element) where T : Attribute
162 return (T[]) Attribute.GetCustomAttributes (element, typeof (T));
165 public static IEnumerable<T> GetCustomAttributes<T> (this MemberInfo element) where T : Attribute
167 return (T[]) Attribute.GetCustomAttributes (element, typeof (T));
170 public static IEnumerable<T> GetCustomAttributes<T> (this MemberInfo element, bool inherit) where T : Attribute
172 return (T[]) Attribute.GetCustomAttributes (element, typeof (T), inherit);
175 public static IEnumerable<T> GetCustomAttributes<T> (this Module element) where T : Attribute
177 return (T[]) Attribute.GetCustomAttributes (element, typeof (T));
180 public static IEnumerable<T> GetCustomAttributes<T> (this ParameterInfo element) where T : Attribute
182 return (T[]) Attribute.GetCustomAttributes (element, typeof (T));
185 public static IEnumerable<T> GetCustomAttributes<T> (this ParameterInfo element, bool inherit) where T : Attribute
187 return (T[]) Attribute.GetCustomAttributes (element, typeof (T), inherit);
190 public static bool IsDefined (this Assembly element, Type attributeType)
192 return Attribute.IsDefined (element, attributeType);
195 public static bool IsDefined (this MemberInfo element, Type attributeType)
197 return Attribute.IsDefined (element, attributeType);
200 public static bool IsDefined (this Module element, Type attributeType)
202 return Attribute.IsDefined (element, attributeType);
205 public static bool IsDefined (this ParameterInfo element, Type attributeType)
207 return Attribute.IsDefined (element, attributeType);
210 public static bool IsDefined (this MemberInfo element, Type attributeType, bool inherit)
212 return Attribute.IsDefined (element, attributeType, inherit);
215 public static bool IsDefined (this ParameterInfo element, Type attributeType, bool inherit)
217 return Attribute.IsDefined (element, attributeType, inherit);