RpcParameters.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. namespace JsonRPC4.Router
  6. {
  7. public class RpcParameters
  8. {
  9. public object Value
  10. {
  11. get;
  12. }
  13. public bool IsDictionary
  14. {
  15. get;
  16. }
  17. public Dictionary<string, IRpcParameter> AsDictionary
  18. {
  19. get
  20. {
  21. CheckValue(isDictionary: true);
  22. return (Dictionary<string, IRpcParameter>)Value;
  23. }
  24. }
  25. public List<IRpcParameter> AsList
  26. {
  27. get
  28. {
  29. CheckValue(isDictionary: false);
  30. return (List<IRpcParameter>)Value;
  31. }
  32. }
  33. public RpcParameters(Dictionary<string, IRpcParameter> parameters)
  34. {
  35. Value = (parameters ?? throw new ArgumentNullException("parameters"));
  36. IsDictionary = true;
  37. }
  38. public RpcParameters(List<IRpcParameter> parameters)
  39. {
  40. Value = (parameters ?? throw new ArgumentNullException("parameters"));
  41. IsDictionary = false;
  42. }
  43. public RpcParameters(params IRpcParameter[] parameters)
  44. {
  45. Value = (parameters?.ToList() ?? throw new ArgumentNullException("parameters"));
  46. IsDictionary = false;
  47. }
  48. private void CheckValue(bool isDictionary)
  49. {
  50. if (isDictionary != IsDictionary)
  51. {
  52. throw new InvalidOperationException();
  53. }
  54. }
  55. }
  56. }