using System; using System.Collections.Generic; using System.Linq; using Logging; namespace FrontendRouting { public static class FeRoutingViewsFinder { // ReSharper disable once StaticMemberInGenericType private static Dictionary _all; private static Dictionary FindAllAttr() { var tFerv = typeof(FeRoutingView); var types = typeof(TAssembly).Assembly.GetTypes().Where(p => false == p.IsAbstract && p.IsSubclassOf(tFerv)); var all = new Dictionary(); foreach (var type in types) { var attrs = type.GetCustomAttributes(typeof(FeRouteAttribute), false).Cast().ToArray(); if (0 == attrs.Length) { Logger.Warn($"No route attribute for {type.FullName}"); } else { all[type] = attrs.ToArray(); } } return all; } public static ICollection AllAttributes { get { if (null == _all) _all = FindAllAttr(); return _all.SelectMany(p => p.Value).ToArray(); } } public static void RegisterAllView() { if (null == _all) _all = FindAllAttr(); foreach (var kvp in _all) { var instance = (FeRoutingView)Activator.CreateInstance(kvp.Key); foreach (var routeAttribute in kvp.Value) { FeRoutingManager.Register(routeAttribute.Path, instance); } } } } }