GetAttributes
Gets an enumeration of assembly attributes of the specified type from the assembly it is called from.
Source
using System;
using System.Collections.Generic;
using System.Reflection;
/// <summary>
/// Extension methods for dealing with an assembly
/// </summary>
public static class AssemblyExtensions
{
/// <summary>
/// Gets the attributes from an assembly.
/// </summary>
/// <typeparam name="T">The type of the custom attribute to find.</typeparam>
/// <param name="callingAssembly">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 Assembly callingAssembly)
where T : Attribute
{
// Try to find the configuration attribute for the default logger if it exists
object[] configAttributes = Attribute.GetCustomAttributes(callingAssembly,
typeof(T), false);
if (configAttributes != null)
{
foreach (T attribute in configAttributes)
{
yield return attribute;
}
}
}
}
Example
var attributes = Assembly.GetExecutingAssembly().GetAttributes<InstallPerformanceCounterAttribute>();
Author: James Michael Hare (BlackRabbitCoder)
Submitted on: 14 okt. 2010
Language: C#
Type: System.Reflection.Assembly
Views: 5350