MemberInfoExtensions
Extensions to the various MemberInfo derived classes
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace NUnit.Compatibility
{
public static class MemberInfoExtensions
{
public static MethodInfo GetGetMethod(this PropertyInfo pinfo, bool nonPublic)
{
if ((object)pinfo.GetMethod == null)
return null;
if (!nonPublic && pinfo.GetMethod.IsPrivate)
return null;
return pinfo.GetMethod;
}
public static IEnumerable<T> GetAttributes<T>(this MemberInfo info, bool inherit) where T : class
{
return GetAttributesImpl<T>(CustomAttributeExtensions.GetCustomAttributes(info, inherit));
}
public static IEnumerable<T> GetAttributes<T>(this ParameterInfo info, bool inherit) where T : class
{
return GetAttributesImpl<T>(CustomAttributeExtensions.GetCustomAttributes(info, inherit));
}
public static IEnumerable<T> GetAttributes<T>(this Assembly asm) where T : class
{
return GetAttributesImpl<T>(asm.GetCustomAttributes());
}
private static IEnumerable<T> GetAttributesImpl<T>(IEnumerable<Attribute> attributes) where T : class
{
List<T> attrs = (List<T>)new List<T>();
(from a in attributes
where typeof(T).IsAssignableFrom(a.GetType())
select a).All(delegate(Attribute a) {
((List<T>)attrs).Add(a as T);
return true;
});
return (IEnumerable<T>)attrs;
}
}
}