The source code is also available on MSDN here: http://msdn.microsoft.com/en-us/library/system.reflection.icustomattributeprovider.getcustomattributes(v=vs.90).aspx
- T GetCustomAttribute(bool)
- T GetCustomAttribute()
- T[] GetCustomAttributes(bool)
- T[] GetCustomAttributes()
- bool TryGetCustomAttribute(bool, out T)
- bool TryGetCustomAttribute(out T)
- bool TryGetCustomAttributes(bool, out T[])
- bool TryGetCustomAttributes(out T[])
using System; using System.Reflection; namespace Extensions { public static class Reflection { #region ICustomAttributeProvider /// <summary> /// Returns the first custom attributes defined on this member, identified by type T, or null if there are no custom attributes of that type. /// </summary> /// <typeparam name="T">Type of the custom attributes to extract.</typeparam> /// <param name="customAttributeProvider">Provider to extract the custom attribute from.</param> /// <param name="inherit">When true, look up the hierarchy chain for the inherited custom attribute.</param> /// <returns>An single custom attributes located on index zero in the custom attribute array, or null.</returns> /// <remarks>Developered by Paw Jershauge for rapid Custom Attribute extraction.</remarks> public static T GetCustomAttribute<T>(this ICustomAttributeProvider customAttributeProvider, bool inherit) where T : Attribute { T outval = null; if (TryGetCustomAttribute<T>(customAttributeProvider, inherit, out outval)) return outval; return null; } /// <summary> /// Returns the first custom attributes defined on this member, identified by type T, or null if there are no custom attributes of that type. /// </summary> /// <typeparam name="T">Type of the custom attributes to extract.</typeparam> /// <param name="customAttributeProvider">Provider to extract the custom attribute from.</param> /// <returns>An single custom attributes located on index zero in the custom attribute array, or null.</returns> /// <remarks>Developered by Paw Jershauge for rapid Custom Attribute extraction.</remarks> public static T GetCustomAttribute<T>(this ICustomAttributeProvider customAttributeProvider) where T : Attribute { T outval = null; if (TryGetCustomAttribute<T>(customAttributeProvider, true, out outval)) return outval; return null; } /// <summary> /// Returns an array of custom attributes defined on this member, identified by type T, or an empty array if there are no custom attributes of that type. /// </summary> /// <typeparam name="T">Type of the custom attributes to extract.</typeparam> /// <param name="customAttributeProvider">Provider to extract the custom attribute from.</param> /// <param name="inherit">When true, look up the hierarchy chain for the inherited custom attribute.</param> /// <returns>An array of custom attributes, or an empty custom attribute array.</returns> /// <remarks>Developered by Paw Jershauge for rapid Custom Attribute extraction.</remarks> public static T[] GetCustomAttributes<T>(this ICustomAttributeProvider customAttributeProvider, bool inherit) where T : Attribute { T[] outval = null; if (TryGetCustomAttributes<T>(customAttributeProvider, inherit, out outval)) return outval; return new T[0]; } /// <summary> /// Returns an array of custom attributes defined on this member, identified by type T, or an empty array if there are no custom attributes of that type. /// </summary> /// <typeparam name="T">Type of the custom attributes to extract.</typeparam> /// <param name="customAttributeProvider">Provider to extract the custom attribute from.</param> /// <returns>An array of custom attributes, or an empty custom attribute array.</returns> /// <remarks>Developered by Paw Jershauge for rapid Custom Attribute extraction.</remarks> public static T[] GetCustomAttributes<T>(this ICustomAttributeProvider customAttributeProvider) where T : Attribute { T[] outval = null; if (TryGetCustomAttributes<T>(customAttributeProvider, true, out outval)) return outval; return new T[0]; } /// <summary> /// Extracts the first custom attributes defined on this member, identified by type T, or null if there are no custom attributes of that type. /// A return value indicates whether the extraction was successfull or failed. /// </summary> /// <typeparam name="T">Type of the custom attributes to extract.</typeparam> /// <param name="customAttributeProvider">Provider to extract the custom attribute from.</param> /// <param name="inherit">When true, look up the hierarchy chain for the inherited custom attribute.</param> /// <param name="value">When this method returns, contains the custom attribute value contained in customAttributeProvider, if the extraction succeeded, or null if the extraction failed. /// The extraction fails if the customAttributeProvider parameter is null, or customAttributeProvider does not contain the custom attribute.</param> /// <returns>true if the extraction was successfully; otherwise, false.</returns> /// <remarks>Developered by Paw Jershauge for rapid Custom Attribute extraction.</remarks> public static bool TryGetCustomAttribute<T>(this ICustomAttributeProvider customAttributeProvider, bool inherit, out T value) where T : Attribute { value = null; if (customAttributeProvider == null) return false; object[] attributes = customAttributeProvider.GetCustomAttributes(typeof(T), inherit); if (attributes == null) return false; if (attributes.Length == 0) return false; value = (T)attributes[0]; return true; } /// <summary> /// Extracts the first custom attributes defined on this member, identified by type T, or null if there are no custom attributes of that type. /// A return value indicates whether the extraction was successfull or failed. /// </summary> /// <typeparam name="T">Type of the custom attributes to extract.</typeparam> /// <param name="customAttributeProvider">Provider to extract the custom attribute from.</param> /// <param name="value">When this method returns, contains the custom attribute value contained in customAttributeProvider, if the extraction succeeded, or null if the extraction failed. /// The extraction fails if the customAttributeProvider parameter is null, or customAttributeProvider does not contain the custom attribute.</param> /// <returns>true if the extraction was successfully; otherwise, false.</returns> /// <remarks>Developered by Paw Jershauge for rapid Custom Attribute extraction.</remarks> public static bool TryGetCustomAttribute<T>(this ICustomAttributeProvider customAttributeProvider, out T value) where T : Attribute { return TryGetCustomAttribute<T>(customAttributeProvider, true, out value); } /// <summary> /// Extracts an array of custom attributes defined on this member, identified by type T, or an empty array if there are no custom attributes of that type. /// A return value indicates whether the extraction was successfull or failed. /// </summary> /// <typeparam name="T">Type of the custom attributes to extract.</typeparam> /// <param name="customAttributeProvider">Provider to extract the custom attribute from.</param> /// <param name="inherit">When true, look up the hierarchy chain for the inherited custom attribute.</param> /// <param name="value">When this method returns, contains the custom attribute array contained in customAttributeProvider, if the extraction succeeded, or null if the extraction failed. /// The extraction fails if the customAttributeProvider parameter is null, or customAttributeProvider does not contain the custom attribute.</param> /// <returns>true if the extraction was successfully; otherwise, false.</returns> /// <remarks>Developered by Paw Jershauge for rapid Custom Attribute extraction.</remarks> public static bool TryGetCustomAttributes<T>(this ICustomAttributeProvider customAttributeProvider, bool inherit, out T[] value) where T : Attribute { value = new T[0]; if (customAttributeProvider == null) return false; object[] attributes = customAttributeProvider.GetCustomAttributes(typeof(T), inherit); if (attributes == null) return false; if (attributes.Length == 0) return false; value = new T[attributes.Length]; for (int i = 0; i < attributes.Length; i++) value[i] = attributes[0] as T; return true; } /// <summary> /// Extracts an array of custom attributes defined on this member, identified by type T, or an empty array if there are no custom attributes of that type. /// A return value indicates whether the extraction was successfull or failed. /// </summary> /// <typeparam name="T">Type of the custom attributes to extract.</typeparam> /// <param name="customAttributeProvider">Provider to extract the custom attribute from.</param> /// <param name="value">When this method returns, contains the custom attribute array contained in customAttributeProvider, if the extraction succeeded, or null if the extraction failed. /// The extraction fails if the customAttributeProvider parameter is null, or customAttributeProvider does not contain the custom attribute.</param> /// <returns>true if the extraction was successfully; otherwise, false.</returns> /// <remarks>Developered by Paw Jershauge for rapid Custom Attribute extraction.</remarks> public static bool TryGetCustomAttributes<T>(this ICustomAttributeProvider customAttributeProvider, out T[] value) where T : Attribute { return TryGetCustomAttributes<T>(customAttributeProvider, true, out value); } #endregion } }