12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Logging;
- namespace FrontendRouting
- {
- public static class FeRoutingViewsFinder<TAssembly>
- {
- // ReSharper disable once StaticMemberInGenericType
- private static Dictionary<Type, FeRouteAttribute[]> _all;
- private static Dictionary<Type, FeRouteAttribute[]> FindAllAttr()
- {
- var tFerv = typeof(FeRoutingView);
- var types = typeof(TAssembly).Assembly.GetTypes().Where(p => false == p.IsAbstract && p.IsSubclassOf(tFerv));
- var all = new Dictionary<Type, FeRouteAttribute[]>();
- foreach (var type in types)
- {
- var attrs = type.GetCustomAttributes(typeof(FeRouteAttribute), false).Cast<FeRouteAttribute>().ToArray();
- if (0 == attrs.Length)
- {
- Logger.Warn($"No route attribute for {type.FullName}");
- }
- else
- {
- all[type] = attrs.ToArray();
- }
- }
- return all;
- }
- public static ICollection<FeRouteAttribute> 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);
- }
- }
- }
- }
- }
|