ICustomAttributeProvider.GetCustomAttributes Method
.NET Framework 3.5
Returns custom attributes defined on this member.
This member is overloaded. For complete information about this member, including syntax, usage, and examples, click a name in the overload list.
| Name | Description | |
|---|---|---|
|
GetCustomAttributes(Boolean) | Returns an array of all of the custom attributes defined on this member, excluding named attributes, or an empty array if there are no custom attributes. |
|
GetCustomAttributes(Type, Boolean) | Returns an array of custom attributes defined on this member, identified by type, or an empty array if there are no custom attributes of that type. |
8 ICustomAttributeProvider Extensions
Fully descibed ICustomAttributeProvider Extensions
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
}
}
- 1/19/2011
- Paw Jershauge
Generic extensions for retrieving custom attributes
As most of the Reflection classes, such as Type, MemberInfo, and ParameterInfo, all implement ICustomAttributeProvider, you can add some extension methods (new to C# 3.0 and Visual Basic 9.0) to extend this interface to make retrieving attributes a little easier.
For example:
[C#]
using System;
using System.Reflection;
public static class CustomAttributeProviderExtensions
{
public static T[] GetCustomAttributes<T>(this ICustomAttributeProvider provider) where T : Attribute
{
return GetCustomAttributes<T>(provider, true);
}
public static T[] GetCustomAttributes<T>(this ICustomAttributeProvider provider, bool inherit) where T : Attribute
{
if (provider == null)
throw new ArgumentNullException("provider");
T[] attributes = provider.GetCustomAttributes(typeof(T), inherit) as T[];
if (attributes == null)
{ // WORKAROUND: Due to a bug in the code for retrieving attributes
// from a dynamic generated parameter, GetCustomAttributes can return
// an instance of an object[] instead of T[], hence the cast above
// will return null.
return new T[0];
}
return attributes;
}
}
This allows you to do the following:
[C#]
[Serializable]
class Program
{
static void Main(string[] args)
{
SerializableAttribute[] attributes = typeof(Program).GetCustomAttributes<SerializableAttribute>();
foreach (var attribute in attributes)
{
Console.WriteLine(attribute);
}
}
}
Which outputs the following:
SerializableAttribute
- 11/23/2008
- David M. Kean
- 7/13/2009
- David M. Kean