2020
2121using System ;
2222using System . Collections . Generic ;
23+ using System . Diagnostics . Contracts ;
2324using System . Linq ;
2425using System . Reflection ;
2526
@@ -194,6 +195,26 @@ public static bool IsDefined( this Type source, Type attributeType )
194195 {
195196 return source . GetTypeInfo ( ) . IsDefined ( attributeType ) ;
196197 }
198+
199+ public static IEnumerable < CustomAttributeData > GetCustomAttributesData ( this Type source )
200+ {
201+ return source . GetTypeInfo ( ) . CustomAttributes ;
202+ }
203+
204+ public static IEnumerable < CustomAttributeData > GetCustomAttributesData ( this MemberInfo source )
205+ {
206+ return source . CustomAttributes ;
207+ }
208+
209+ public static Type GetAttributeType ( this CustomAttributeData source )
210+ {
211+ return source . AttributeType ;
212+ }
213+
214+ public static string GetMemberName ( this CustomAttributeNamedArgument source )
215+ {
216+ return source . MemberName ;
217+ }
197218#else
198219 public static bool IsDefined ( this MemberInfo source , Type attributeType )
199220 {
@@ -204,7 +225,100 @@ public static T GetCustomAttribute<T>( this MemberInfo source )
204225 where T : Attribute
205226 {
206227 return Attribute . GetCustomAttribute ( source , typeof ( T ) ) as T ;
207- }
228+ }
229+
230+ #if ! SILVERLIGHT
231+ public static Type GetAttributeType ( this CustomAttributeData source )
232+ {
233+ return source . Constructor . DeclaringType ;
234+ }
235+
236+ public static string GetMemberName ( this CustomAttributeNamedArgument source )
237+ {
238+ return source . MemberInfo . Name ;
239+ }
240+
241+ #else
242+ public static Type GetAttributeType ( this Attribute source )
243+ {
244+ return source . GetType ( ) ;
245+ }
246+ #endif // !SILVERLIGHT
247+ #endif
248+
249+ #if NETFX_35
250+ public static IEnumerable < CustomAttributeData > GetCustomAttributesData ( this MemberInfo source )
251+ {
252+ return CustomAttributeData . GetCustomAttributes ( source ) ;
253+ }
254+ #endif // NETFX_35
255+
256+ #if SILVERLIGHT
257+ public static IEnumerable < Attribute > GetCustomAttributesData ( this MemberInfo source )
258+ {
259+ return source . GetCustomAttributes ( false ) . OfType < Attribute > ( ) ;
260+ }
261+
262+ public static IEnumerable < NamedArgument > GetNamedArguments ( this Attribute attribute )
263+ {
264+ return
265+ attribute . GetType ( )
266+ . GetMembers ( BindingFlags . Public | BindingFlags . Instance )
267+ . Where ( m => m . MemberType == MemberTypes . Field || m . MemberType == MemberTypes . Property )
268+ . Select ( m => new NamedArgument ( attribute , m ) ) ;
269+ }
270+ #else
271+ public static IEnumerable < CustomAttributeNamedArgument > GetNamedArguments ( this CustomAttributeData source )
272+ {
273+ return source . NamedArguments ;
274+ }
275+
276+ public static CustomAttributeTypedArgument GetTypedValue ( this CustomAttributeNamedArgument source )
277+ {
278+ return source . TypedValue ;
279+ }
280+ #endif // SILVERLIGHT
281+
282+ #if SILVERLIGHT
283+ public struct NamedArgument
284+ {
285+ private object _instance ;
286+ private MemberInfo _memberInfo ;
287+
288+ public NamedArgument ( object instance , MemberInfo memberInfo )
289+ {
290+ this . _instance = instance ;
291+ this . _memberInfo = memberInfo ;
292+ }
293+
294+ public string GetMemberName ( )
295+ {
296+ return this . _memberInfo . Name ;
297+ }
298+
299+ public KeyValuePair < Type , object > GetTypedValue ( )
300+ {
301+ Type type ;
302+ object value ;
303+ PropertyInfo asProperty ;
304+ if ( ( asProperty = this . _memberInfo as PropertyInfo ) != null )
305+ {
306+ type = asProperty . PropertyType ;
307+ value = asProperty . GetValue ( this . _instance , null ) ;
308+ }
309+ else
310+ {
311+ var asField = this . _memberInfo as FieldInfo ;
312+ #if DEBUG
313+ Contract . Assert ( asField != null ) ;
314+ #endif
315+ type = asField . FieldType ;
316+ value = asField . GetValue ( this . _instance ) ;
317+ }
318+
319+ return new KeyValuePair < Type , object > ( type , value ) ;
320+ }
321+ }
208322#endif
209323 }
210324}
0 commit comments