A few extension methods to help out.....
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
public static class AttributeHelpers
{
public static T GetCustomAttribute<T>(this ICustomAttributeProvider provider)
where T : Attribute
{
return GetCustomAttribute<T>(provider, false);
}
public static T GetCustomAttribute<T>(this ICustomAttributeProvider provider, bool inherited)
where T : Attribute
{
return provider.GetCustomAttributes<T>(inherited).FirstOrDefault();
}
public static List<T> GetCustomAttributes<T>(this ICustomAttributeProvider provider)
where T : Attribute
{
return GetCustomAttributes<T>(provider, false);
}
public static List<T> GetCustomAttributes<T>(this ICustomAttributeProvider provider, bool inherited)
where T : Attribute
{
return provider.GetCustomAttributes(typeof (T), inherited).Cast<T>().ToList();
}
}
Thanks to Wouter Ballet.
To tonyt: Regarding linq abuse, although I have factored out the linq expression you complained about, it was intentional to deal with a casting issue. I invite you to check the previous revision and make that expression work in the order you propose without a lot of unnecessary bloat. But thanks for the feedback.
My name is Sky Sanders and I am an end-to-end, front-to-back software solutions architect with more than 20 years experience in IT infrastructure and software development, the last 10 years being focused primarily on the Microsoft .NET platform.
My motto is 'I solve problems.' and I am currently available for hire.
I can be contacted at sky.sanders@gmail.com