CallingMethod.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. namespace MiniWebApi.Handler
  4. {
  5. /// <summary>
  6. /// CallingMethod contains the WebApi method, Call the Call method to call the WebApi.
  7. /// </summary>
  8. public class CallingMethod
  9. {
  10. /// <summary>
  11. /// The this object for call, the class method need the this object for the first argument.
  12. /// </summary>
  13. private readonly object _thisObject;
  14. /// <summary>
  15. /// The WebApi to call.
  16. /// </summary>
  17. private readonly IWebApiMethod _method;
  18. /// <summary>
  19. /// Gets the name of the method.
  20. /// </summary>
  21. public string Name { get; }
  22. /// <summary>
  23. /// Gets the information for the parameters.
  24. /// </summary>
  25. public IReadOnlyList<CallingParameter> Parameters { get; }
  26. /// <summary>
  27. /// Gets the WebApiType of the method.
  28. /// </summary>
  29. public WebApiType WebApiType { get; }
  30. /// <summary>
  31. /// Create the CallingMethod.
  32. /// </summary>
  33. /// <param name="name">The name of the method.</param>
  34. /// <param name="webApiType">The WebApi Type of the method.</param>
  35. /// <param name="parameters">The parameters information of the method.</param>
  36. /// <param name="thisObject">The object which contains the WebApi Method.</param>
  37. /// <param name="method">The method to call the WebApi.</param>
  38. public CallingMethod(string name, WebApiType webApiType, IEnumerable<CallingParameter> parameters, object thisObject, IWebApiMethod method)
  39. {
  40. Name = name;
  41. WebApiType = webApiType;
  42. Parameters = parameters.ToList();
  43. _thisObject = thisObject;
  44. _method = method;
  45. }
  46. /// <summary>
  47. /// Call the WebApi method.
  48. /// </summary>
  49. /// <param name="webApiArguments"></param>
  50. public void Call(WebApiArgument[] webApiArguments)
  51. {
  52. var args = webApiArguments.Select(x => x.Value).ToArray();
  53. _method.Call(_thisObject, args);
  54. }
  55. }
  56. }