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

namespace Villain.Reflection
{
	public static class Reflector
	{
		public static IEnumerable<MethodByAttribute> FindMethodsByAttribute<TAttribute>() where TAttribute : Attribute
		{
			return (
				from method
				in AppDomain.CurrentDomain.GetAssemblies().Where(assembly => !assembly.GlobalAssemblyCache).SelectMany(assembly => assembly.GetTypes()).SelectMany(type => type.GetMethods())
				let attribute = Attribute.GetCustomAttribute(method, typeof(TAttribute), false) as TAttribute
				where attribute != null
				select new MethodByAttribute(attribute, method)
			);
		}
	}
}