FeRoutingViewsFinder.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Logging;
  5. namespace FrontendRouting
  6. {
  7. public static class FeRoutingViewsFinder<TAssembly>
  8. {
  9. // ReSharper disable once StaticMemberInGenericType
  10. private static Dictionary<Type, FeRouteAttribute[]> _all;
  11. private static Dictionary<Type, FeRouteAttribute[]> FindAllAttr()
  12. {
  13. var tFerv = typeof(FeRoutingView);
  14. var types = typeof(TAssembly).Assembly.GetTypes().Where(p => false == p.IsAbstract && p.IsSubclassOf(tFerv));
  15. var all = new Dictionary<Type, FeRouteAttribute[]>();
  16. foreach (var type in types)
  17. {
  18. var attrs = type.GetCustomAttributes(typeof(FeRouteAttribute), false).Cast<FeRouteAttribute>().ToArray();
  19. if (0 == attrs.Length)
  20. {
  21. Logger.Warn($"No route attribute for {type.FullName}");
  22. }
  23. else
  24. {
  25. all[type] = attrs.ToArray();
  26. }
  27. }
  28. return all;
  29. }
  30. public static ICollection<FeRouteAttribute> AllAttributes
  31. {
  32. get
  33. {
  34. if (null == _all) _all = FindAllAttr();
  35. return _all.SelectMany(p => p.Value).ToArray();
  36. }
  37. }
  38. public static void RegisterAllView()
  39. {
  40. if (null == _all) _all = FindAllAttr();
  41. foreach (var kvp in _all)
  42. {
  43. var instance = (FeRoutingView)Activator.CreateInstance(kvp.Key);
  44. foreach (var routeAttribute in kvp.Value)
  45. {
  46. FeRoutingManager.Register(routeAttribute.Path, instance);
  47. }
  48. }
  49. }
  50. }
  51. }