You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
2.1 KiB

4 years ago
2 years ago
4 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. namespace Apewer.Web
  5. {
  6. internal sealed class ApiFunction
  7. {
  8. internal ApiApplication Application;
  9. internal MethodInfo Method;
  10. internal Type Returnable;
  11. internal ParameterInfo[] Parameters;
  12. internal bool ParamIsRecord = false;
  13. // 主特性和主要属性。
  14. // internal ApiAttribute Attribute;
  15. internal string Name = null;
  16. internal string Lower = null;
  17. // 附加特性。
  18. internal bool Hidden;
  19. internal string Caption;
  20. internal string Description;
  21. private Class<Json> psJson = null;
  22. internal Json ToJson(ApiOptions options)
  23. {
  24. if (Hidden) return null;
  25. var json = Json.NewObject();
  26. json.SetProperty("name", Name);
  27. if (!string.IsNullOrEmpty(Caption)) json.SetProperty("caption", Caption);
  28. if (!string.IsNullOrEmpty(Description)) json.SetProperty("description", Description);
  29. if (options.WithParameters)
  30. {
  31. if (psJson == null)
  32. {
  33. if (Parameters == null || Parameters.Length < 0)
  34. {
  35. psJson = new Class<Json>();
  36. }
  37. else
  38. {
  39. var ps = Json.NewArray();
  40. var psList = new List<ParameterInfo>();
  41. psList.AddRange(Parameters);
  42. psList.Sort(new Comparison<ParameterInfo>((a, b) => a.Name.CompareTo(b.Name)));
  43. foreach (var pi in psList)
  44. {
  45. var p = Json.NewObject();
  46. p.SetProperty("name", pi.Name);
  47. p.SetProperty("type", pi.ParameterType.Name);
  48. ps.AddItem(p);
  49. }
  50. psJson = new Class<Json>(ps);
  51. }
  52. }
  53. if (psJson) json.SetProperty("parameters", psJson.Value);
  54. }
  55. return json;
  56. }
  57. }
  58. }