ServiceModule.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Reflection;
  7. namespace ServerLib
  8. {
  9. public class ServiceModule
  10. {
  11. public bool SearchRoute(ServiceRoute route)
  12. {
  13. return true;
  14. }
  15. public ActionResult ExecuteRoute(ServiceRoute route)
  16. {
  17. var type = this.GetType();
  18. var methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance);
  19. methods = methods.Where(m => m.ReturnType == typeof(ActionResult)).ToArray();
  20. if (methods == null || methods.Length <= 0) return null;
  21. var method = methods.FirstOrDefault(m =>
  22. {
  23. var attribute = m.GetCustomAttribute<RouteAttribute>(true);
  24. if (attribute == null) return false;
  25. if (attribute.Method == route.Method && attribute.RoutePath == route.RoutePath)
  26. return true;
  27. return false;
  28. });
  29. if (method == null) return null;
  30. return (ActionResult)method.Invoke(this, new object[] { });
  31. }
  32. }
  33. }