ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

GetAttribute

Makes it easier to retrieve custom attributes of a given type from a reflected type.

Source

using System;
using System.Collections.Generic;
using System.Linq;


/// <summary>
/// Extension methods for the Type class
/// </summary>
public static class TypeExtensions
{
	/// <summary>
	/// Loads the custom attributes from the type
	/// </summary>
	/// <typeparam name="T">The type of the custom attribute to find.</typeparam>
	/// <param name="typeWithAttributes">The calling assembly to search.</param>
	/// <returns>The custom attribute of type T, if found.</returns>
	public static T GetAttribute<T>(this Type typeWithAttributes)
		where T : Attribute
	{
		return GetAttributes<T>(typeWithAttributes).FirstOrDefault();
	}

	/// <summary>
	/// Loads the custom attributes from the type
	/// </summary>
	/// <typeparam name="T">The type of the custom attribute to find.</typeparam>
	/// <param name="typeWithAttributes">The calling assembly to search.</param>
	/// <returns>An enumeration of attributes of type T that were found.</returns>
	public static IEnumerable<T> GetAttributes<T>(this Type typeWithAttributes)
		where T : Attribute
	{
		// Try to find the configuration attribute for the default logger if it exists
		object[] configAttributes = Attribute.GetCustomAttributes(typeWithAttributes,
			typeof(T), false);

		if (configAttributes != null)
		{
			foreach (T attribute in configAttributes)
			{
				yield return attribute;
			}
		}
	}
}

Example

var attribute = typeof(SomeType).GetAttribute<XmlSerializationAttribute>();

Author: James Michael Hare (Black Rabbit Coder)

Submitted on: 14 okt. 2010

Language: C#

Type: System.Type

Views: 10627