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.
31 using System.Collections.Generic;
33 namespace System.Reflection
35 public static class CustomAttributeExtensions
37 public static T GetCustomAttribute<T> (this Assembly element) where T : Attribute
39 return (T) Attribute.GetCustomAttribute (element, typeof (T));
42 public static T GetCustomAttribute<T> (this MemberInfo element) where T : Attribute
44 return (T) Attribute.GetCustomAttribute (element, typeof (T));
47 public static T GetCustomAttribute<T> (this Module element) where T : Attribute
49 return (T) Attribute.GetCustomAttribute (element, typeof (T));
52 public static T GetCustomAttribute<T> (this ParameterInfo element) where T : Attribute
54 return (T) Attribute.GetCustomAttribute (element, typeof (T));
57 public static T GetCustomAttribute<T> (this MemberInfo element, bool inherit) where T : Attribute
59 return (T) Attribute.GetCustomAttribute (element, typeof (T), inherit);
62 public static T GetCustomAttribute<T> (this ParameterInfo element, bool inherit) where T : Attribute
64 return (T) Attribute.GetCustomAttribute (element, typeof (T), inherit);
67 public static Attribute GetCustomAttribute (this Assembly element, Type attributeType)
69 return Attribute.GetCustomAttribute (element, attributeType);
72 public static Attribute GetCustomAttribute (this MemberInfo element, Type attributeType)
74 return Attribute.GetCustomAttribute (element, attributeType);
77 public static Attribute GetCustomAttribute (this MemberInfo element, Type attributeType, bool inherit)
79 return Attribute.GetCustomAttribute (element, attributeType, inherit);
82 public static Attribute GetCustomAttribute (this Module element, Type attributeType)
84 return Attribute.GetCustomAttribute (element, attributeType);
87 public static Attribute GetCustomAttribute (this ParameterInfo element, Type attributeType)
89 return Attribute.GetCustomAttribute (element, attributeType);
92 public static Attribute GetCustomAttribute (this ParameterInfo element, Type attributeType, bool inherit)
94 return Attribute.GetCustomAttribute (element, attributeType, inherit);
97 public static IEnumerable<Attribute> GetCustomAttributes (this Assembly element)
99 return Attribute.GetCustomAttributes (element);
102 public static IEnumerable<Attribute> GetCustomAttributes (this Assembly element, Type attributeType)
104 return Attribute.GetCustomAttributes (element, attributeType);
107 public static IEnumerable<Attribute> GetCustomAttributes (this Module element)
109 return Attribute.GetCustomAttributes (element);
112 public static IEnumerable<Attribute> GetCustomAttributes (this Module element, Type attributeType)
114 return Attribute.GetCustomAttributes (element);
117 public static IEnumerable<Attribute> GetCustomAttributes (this ParameterInfo element)
119 return Attribute.GetCustomAttributes (element);
122 public static IEnumerable<Attribute> GetCustomAttributes (this ParameterInfo element, bool inherit)
124 return Attribute.GetCustomAttributes (element, inherit);
127 public static IEnumerable<Attribute> GetCustomAttributes (this ParameterInfo element, Type attributeType)
129 return Attribute.GetCustomAttributes (element, attributeType);
132 public static IEnumerable<Attribute> GetCustomAttributes (this ParameterInfo element, Type attributeType,
135 return Attribute.GetCustomAttributes (element, attributeType, inherit);
138 public static IEnumerable<Attribute> GetCustomAttributes (this MemberInfo element)
140 return Attribute.GetCustomAttributes (element);
143 public static IEnumerable<Attribute> GetCustomAttributes (this MemberInfo element, bool inherit)
145 return Attribute.GetCustomAttributes (element, inherit);
148 public static IEnumerable<Attribute> GetCustomAttributes (this MemberInfo element, Type attributeType)
150 return Attribute.GetCustomAttributes (element, attributeType);
153 public static IEnumerable<Attribute> GetCustomAttributes (this MemberInfo element, Type attributeType,
156 return Attribute.GetCustomAttributes (element, attributeType, inherit);
159 public static IEnumerable<T> GetCustomAttributes<T> (this Assembly element) where T : Attribute
161 return (T[]) Attribute.GetCustomAttributes (element, typeof (T));
164 public static IEnumerable<T> GetCustomAttributes<T> (this MemberInfo element) where T : Attribute
166 return (T[]) Attribute.GetCustomAttributes (element, typeof (T));
169 public static IEnumerable<T> GetCustomAttributes<T> (this MemberInfo element, bool inherit) where T : Attribute
171 return (T[]) Attribute.GetCustomAttributes (element, typeof (T), inherit);
174 public static IEnumerable<T> GetCustomAttributes<T> (this Module element) where T : Attribute
176 return (T[]) Attribute.GetCustomAttributes (element, typeof (T));
179 public static IEnumerable<T> GetCustomAttributes<T> (this ParameterInfo element) where T : Attribute
181 return (T[]) Attribute.GetCustomAttributes (element, typeof (T));
184 public static IEnumerable<T> GetCustomAttributes<T> (this ParameterInfo element, bool inherit) where T : Attribute
186 return (T[]) Attribute.GetCustomAttributes (element, typeof (T), inherit);
189 public static bool IsDefined (this Assembly element, Type attributeType)
191 return Attribute.IsDefined (element, attributeType);
194 public static bool IsDefined (this MemberInfo element, Type attributeType)
196 return Attribute.IsDefined (element, attributeType);
199 public static bool IsDefined (this Module element, Type attributeType)
201 return Attribute.IsDefined (element, attributeType);
204 public static bool IsDefined (this ParameterInfo element, Type attributeType)
206 return Attribute.IsDefined (element, attributeType);
209 public static bool IsDefined (this MemberInfo element, Type attributeType, bool inherit)
211 return Attribute.IsDefined (element, attributeType, inherit);
214 public static bool IsDefined (this ParameterInfo element, Type attributeType, bool inherit)
216 return Attribute.IsDefined (element, attributeType, inherit);