GetAttribute
Gets the first assembly attribute of the specified type from the assembly it is called from.
Source
/// <summary>
/// Extension methods for dealing with an assembly
/// </summary>
public static class AssemblyExtensions
{
/// <summary>
/// Loads the configuration from assembly attributes
/// </summary>
/// <typeparam name="T">The type of the custom attribute to find.</typeparam>
/// <param name="callingAssembly">The calling assembly to search.</param>
/// <returns>The custom attribute of type T, if found.</returns>
public static T GetAttribute<T>(this Assembly callingAssembly)
where T : Attribute
{
T result = null;
// Try to find the configuration attribute for the default logger if it exists
object[] configAttributes = Attribute.GetCustomAttributes(callingAssembly,
typeof(T), false);
// get just the first one
if (!configAttributes.IsNullOrEmpty())
{
result = (T)configAttributes[0];
}
return result;
}
}
Example
var attribute = Assembly.GetExecutingAssembly().GetAttribute<InstallPerformanceCounterAttribute>();
Author: James Michael Hare (BlackRabbitCoder)
Submitted on: 14 okt. 2010
Language: C#
Type: System.Reflection.Assembly
Views: 9364